mysqli_fetch_array仅返回一个结果 [英] mysqli_fetch_array returning only one result

查看:206
本文介绍了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.):

$connection = mysqli_connect($host, $user_name, $password, $database);
if (mysqli_connect_errno($connection)) {
    echo "Failed to connect to MySQL: " . mysqli_connect_error();
}

$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);

mysqli_close($connection);

如您所见,我以一种易于理解的方式打印出了结果,结果是:

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仅返回一个结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆