2024-04-30

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

在 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中文网其它相关文章!

https://www.php.cn/faq/771296.html

发表回复

Your email address will not be published. Required fields are marked *