循环 php mysqli 获取数组函数
mysqli fetch 函数用于从数据库服务器访问数据。获取数据后,你还可以遍历 mysqli
查询。
在本文中,我们将看到 mysqli_fetch_array()
函数的使用以及迭代访问数据的方法。
这个扩展是在 php 5.0.0 版本中引入的,设计它的目的是与 mysql 4.1.13 或更高版本一起工作。我们在本教程中使用 php 版本 7.4.1 和 phpmyadmin。
你可以从其官方网站下载 xampp。 (如果你有 xampp,则不必单独安装 mysql 和 php)。
迭代 mysqli_fetch_array()
函数
mysqli_fetch_array()
用于使用 $result
作为第一个参数从数据库中检索当前行的数据,将输出保存为关联数组、数值数组或两者(取决于第二个参数)。现在,我们的学生数据库中有以下数据。
mysqli_fetch_array()
函数使用 mysqli_num
模式
让我们编写以下程序,使用 mysqli_fetch_array()
函数从名为 db_students
的学生数据库中读取数据。以下代码将连接数据库并在失败时显示失败消息。
与数据库成功连接后,它将使用 mysqli_query
函数读取记录并将它们保存到 $result
变量中。mysqli_fetch_array()
将使用该结果变量和 mysqli_num
(它的行为类似于 mysqli_fetch_row() 函数)作为参数,以将当前行显示为数字数组,其中索引范围从 0
到 n-1
。
示例代码:
php
$host = "localhost";
$username = "root";
$password = "";
$database = "db_students";
$connection = mysqli_connect($host, $username, $password, $database);
if (mysqli_connect_errno()) {
echo "database connection failed.";
}
$sql = "select * from tb_students";
$result = mysqli_query($connection, $sql);
$row = mysqli_fetch_array($result,mysqli_num);
print_r($row)
?>
输出:
mysqli_fetch_array()
函数使用 mysqli_assoc
模式
在这里,由于参数 mysqli_assoc
,mysqli_fetch_array()
函数的行为类似于 mysqli_fetch_assoc()
,并且表的列名将显示为数组的索引。练习以下代码并观察输出。
示例代码:
php
$host = "localhost";
$username = "root";
$password = "";
$database = "db_students";
$connection = mysqli_connect($host, $username, $password, $database);
if (mysqli_connect_errno()) {
echo "database connection failed.";
}
$sql = "select * from tb_students";
$result = mysqli_query($connection, $sql);
$row = mysqli_fetch_array($result,mysqli_assoc);
print_r($row)
?>
输出:
mysqli_fetch_array()
函数使用 mysqli_both
模式
使用带有参数 mysqli_both
的 mysqli_fetch_array()
函数会将数据存储到我们可以使用列名和列索引访问的数组中。练习以下代码并查看输出。
示例代码:
php
$host = "localhost";
$username = "root";
$password = "";
$database = "db_students";
$connection = mysqli_connect($host, $username, $password, $database);
if (mysqli_connect_errno()) {
echo "database connection failed.";
}
$sql = "select * from tb_students";
$result = mysqli_query($connection, $sql);
$row = mysqli_fetch_array($result,mysqli_both);
print_r($row)
?>
输出:
循环 mysqli_fetch_array()
函数
我们将使用以下代码迭代 mysqli_query
并将输出与学生表记录进行比较。
请记住,你可以循环 mysqli_fetch_array()
、mysqli_assoc
、mysqli_num
和 mysqli_both
的所有模式。使用 mysqli_fetch_array()
时,你必须将其作为参数传递。
php
$host = "localhost";
$username = "root";
$password = "";
$database = "db_students";
$connection = mysqli_connect($host, $username, $password, $database);
if (mysqli_connect_errno()) {
echo "database connection failed.";
}
$sql = "select * from tb_students";
$result = mysqli_query($connection, $sql);
$std_num=0;
while($row = mysqli_fetch_array($result,mysqli_assoc)) {
echo "student number ".$std_num."
";
echo "id: ".$row['id']."
";
echo "first name: ".$row['first_name']."
";
echo "last name: ".$row['last_name']."
";
echo "age: ".$row['age']."
";
echo "
";
$std_num;
}
?>
输出:
你可以查看 id
、first_name
、last_name
、age
并将其与名为 tb_students
的下表进行比较。
结论
上面的讨论得出结论,mysqli_fetch_array()
函数帮助我们从数据库中检索数据。
根据我们的需要和要求,我们可以使用它的输出模式,是否通过列索引或列名访问值,或两者兼而有之。然后,我们可以循环结果来查看表的每条记录。
转载请发邮件至 1244347461@qq.com 进行申请,经作者同意之后,转载请以链接形式注明出处
本文地址:
相关文章
如何在 mysql 中声明和使用变量
发布时间:2024/03/26 浏览次数:115 分类:mysql
-
当你需要在 mysql 中的脚本中存储单个值时,最好的方法是使用变量。变量有不同的种类,有必要知道何时以及如何使用每种类型。
发布时间:2024/03/26 浏览次数:176 分类:mysql
-
本教程演示了如何在 mysql 中重置自动增量。
在 mysql 中使用 mysqladmin 刷新主机解除阻塞
发布时间:2024/03/26 浏览次数:82 分类:mysql
-
你将了解阻止主机的原因。此外,通过使用 phpmyadmin 和命令提示符刷新主机缓存来解除阻塞的不同方法和效果。
发布时间:2024/03/26 浏览次数:199 分类:mysql
-
本教程演示如何在 mysql 中转换为整数。