douzhong5902 2011-07-22 16:32
浏览 35
已采纳

将db查询返回到json_encode的可读关联数组?

I am using ajax to pull some data from a PHP script I created. The PHP script queries the database for user information and comment information. There will be a lot of comments and I am trying to figure out how to return 3 records (LIMIT 3) - this is what I have:

foreach($results->fetch_assoc() as $key => $db_value) {
    //$array[$key] = $db_value;
    print_r($results->fetch_assoc());
}

$results->fetch_assoc() returns a single record set. I was messing around and couldn't figure it out.

What I am trying to do is have 3 arrays returned to the HTML doc via json_encode so that I can read it and display it. Can anyone help?

  • 写回答

1条回答 默认 最新

  • dtvpe4837413 2011-07-22 16:38
    关注
    $data = array();
    
    while ($row = $results->fetch_assoc()) {
       $data[] = $row; // stuff new row onto end of array
    }
    
    echo json_encode($data);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥500 52810做蓝牙接受端
  • ¥15 基于PLC的三轴机械手程序
  • ¥15 多址通信方式的抗噪声性能和系统容量对比
  • ¥15 winform的chart曲线生成时有凸起
  • ¥15 msix packaging tool打包问题
  • ¥15 finalshell节点的搭建代码和那个端口代码教程
  • ¥15 Centos / PETSc / PETGEM
  • ¥15 centos7.9 IPv6端口telnet和端口监控问题
  • ¥20 完全没有学习过GAN,看了CSDN的一篇文章,里面有代码但是完全不知道如何操作
  • ¥15 使用ue5插件narrative时如何切换关卡也保存叙事任务记录