首頁  >  文章  >  後端開發  >  如何使用 PHP 函數從資料庫檢索資料?

如何使用 PHP 函數從資料庫檢索資料?

王林
王林原創
2024-04-30 12:12:01837瀏覽

在PHP 中,可以使用mysqli_query() 函數檢索資料庫數據,並使用mysqli_fetch_row()、mysqli_fetch_assoc() 和mysqli_fetch_array() 函數提取結果以取得行資料,也可以使用mysqli_num_rows() 函數來取得結果集中行的數量。

如何使用 PHP 函数从数据库中检索数据?

如何使用 PHP 函數從資料庫中檢索資料

在 PHP 中,提供了多種函數來操作資料庫並檢索資料。以下是常用的函數beserta 其實戰案例:

mysqli_query() 函數

語法:

mysqli_query(mysqli $link, string $query)

實戰案例:

$link = mysqli_connect("localhost", "root", "password", "my_database");
$query = "SELECT * FROM users";
$result = mysqli_query($link, $query);

mysqli_fetch_row() 函數

語法:

mysqli_fetch_row(mysqli_result $result)

實戰案例:

while ($row = mysqli_fetch_row($result)) {
  echo $row[0] . " " . $row[1] . "\n";
}

mysqli_fetch_assoc() 函數

語法:

mysqli_fetch_assoc(mysqli_result $result)

實戰案例:

while ($row = mysqli_fetch_assoc($result)) {
  echo $row['name'] . " " . $row['email'] . "\n";
}

mysqli_fetch_array() 函數

語法:

mysqli_fetch_array(mysqli_result $result)

實戰案例:

while ($row = mysqli_fetch_array($result)) {
  echo $row[0] . " " . $row['name'] . "\n";
}

mysqli_num_rows() 函數

#語法:

mysqli_num_rows(mysqli_result $result)

實戰案例:

$num_rows = mysqli_num_rows($result);
echo "The number of rows in the result set is: " . $num_rows;

以上是如何使用 PHP 函數從資料庫檢索資料?的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn