mysqli_fetch_array 只返回一个结果

时间:2023-03-07
本文介绍了mysqli_fetch_array 只返回一个结果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

我正在尝试使用以下代码(在 $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);

这篇关于mysqli_fetch_array 只返回一个结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

上一篇:PHP MySQLi 多次插入 下一篇:PHP 准备好的语句问题

相关文章