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 sqlite 附加(attach database)加密数据库时,返回26是什么原因呢?
  • ¥88 找成都本地经验丰富懂小程序开发的技术大咖
  • ¥15 如何处理复杂数据表格的除法运算
  • ¥15 如何用stc8h1k08的片子做485数据透传的功能?(关键词-串口)
  • ¥15 有兄弟姐妹会用word插图功能制作类似citespace的图片吗?
  • ¥200 uniapp长期运行卡死问题解决
  • ¥15 latex怎么处理论文引理引用参考文献
  • ¥15 请教:如何用postman调用本地虚拟机区块链接上的合约?
  • ¥15 为什么使用javacv转封装rtsp为rtmp时出现如下问题:[h264 @ 000000004faf7500]no frame?
  • ¥15 乘性高斯噪声在深度学习网络中的应用