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

王林
發布: 2024-04-30 12:12:01
原創
847 人瀏覽過

在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中文網其他相關文章!

相關標籤:
來源:php.cn
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板
關於我們 免責聲明 Sitemap
PHP中文網:公益線上PHP培訓,幫助PHP學習者快速成長!