doudi7570 2014-10-16 23:31
浏览 309
已采纳

mysqli_fetch_array只返回一个结果

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.

  • 写回答

1条回答 默认 最新

  • douxiao0400 2014-10-16 23:35
    关注

    mysqli_fetch_array works by pointers each time it's called

    Imagine the following

    $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);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 HLs设计手写数字识别程序编译通不过
  • ¥15 Stata外部命令安装问题求帮助!
  • ¥15 从键盘随机输入A-H中的一串字符串,用七段数码管方法进行绘制。提交代码及运行截图。
  • ¥15 TYPCE母转母,插入认方向
  • ¥15 如何用python向钉钉机器人发送可以放大的图片?
  • ¥15 matlab(相关搜索:紧聚焦)
  • ¥15 基于51单片机的厨房煤气泄露检测报警系统设计
  • ¥15 路易威登官网 里边的参数逆向
  • ¥15 Arduino无法同时连接多个hx711模块,如何解决?
  • ¥50 需求一个up主付费课程