Php mysqli_fetch_数组只返回一个结果

Php mysqli_fetch_数组只返回一个结果,php,mysqli,Php,Mysqli,我试图使用以下代码(使用$host等适当的值)对一个小型mysql数据库进行非常简单的查询: 该专栏中有一些大学的例子,所以我不确定我做错了什么。mysqli\u fetch\u数组每次调用时都通过指针工作 想象一下 $result = mysqli_query($connection, "select university from universities_alpha"); $row = mysqli_fetch_array($result); // this is the first ro

我试图使用以下代码(使用$host等适当的值)对一个小型mysql数据库进行非常简单的查询:


该专栏中有一些大学的例子,所以我不确定我做错了什么。

mysqli\u fetch\u数组每次调用时都通过指针工作

想象一下

$result = mysqli_query($connection, "select university from universities_alpha");
$row = mysqli_fetch_array($result); // this is the first row
$row = mysqli_fetch_array($result); // now it's the second row
$row = mysqli_fetch_array($result); // third row
$rows = array();
$result = mysqli_query($connection, "select university from universities_alpha");
while($row = mysqli_fetch_array($result)) {
    $rows[] = $row;
}

print_r($rows);
要以您希望的方式实际显示数据,我建议您执行以下操作

$result = mysqli_query($connection, "select university from universities_alpha");
$row = mysqli_fetch_array($result); // this is the first row
$row = mysqli_fetch_array($result); // now it's the second row
$row = mysqli_fetch_array($result); // third row
$rows = array();
$result = mysqli_query($connection, "select university from universities_alpha");
while($row = mysqli_fetch_array($result)) {
    $rows[] = $row;
}

print_r($rows);

如果要从结果集中获取所有行,则必须使用而不是

将代码替换为以下内容:

$result = mysqli_query($connection, "select university from universities_alpha");
$rows = mysqli_fetch_all($result, MYSQLI_BOTH);

请阅读mysqli功能手册。将
fetch\u数组
放入
while
循环中,因为它逐个返回行,直到返回null,循环终止。