mysqli_fetch_array 只返回一个结果

2021-12-25 00:00:00 php mysqli

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

I'm trying to make a very, very simple query of a small mysql database, using the following code (with appropriate values in $host, etc.):

$result = mysqli_query($connection, "select university from universities_alpha");
$row = mysqli_fetch_array($result);

echo print_r($result);
echo '<br><br>';
echo print_r($row);

如您所见,我以人类可读的方式打印了结果,结果是:

As you can see, I printed out the results in a human-readable way, yielding:

mysqli_result Object ( [current_field] => 0 [field_count] => 1 [lengths] => Array ( [0] => 19 ) [num_rows] => 9 [type] => 0 ) 1

Array ( [0] => Arizona State Univ. [university] => Arizona State Univ. ) 1

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

There are a few example universities in that column, so I'm not sure what I'm doing wrong.

推荐答案

mysqli_fetch_array 每次调用时都通过指针工作

mysqli_fetch_array works by pointers each time it's called

想象以下内容

$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

要以您想要的方式实际显示数据,我建议您执行以下操作

To actually display the data the way you want it to, I suggest you do the following

$rows = array();
$result = mysqli_query($connection, "select university from universities_alpha");
while($row = mysqli_fetch_array($result)) {
    $rows[] = $row;
}

print_r($rows);

相关文章