简体   繁体   English

在多个表上使用PHP / MYSQLi SQL SELECT并将结果作为json对象返回

[英]PHP/MYSQLi SQL SELECT on multiple table and return the result as json object

I am building user status app where other can comment. 我正在构建其他用户可以发表评论的用户状态应用。 I fetch the json result through ajax and then display it in my ap. 我通过ajax获取json结果,然后将其显示在我的ap中。 however my problem is when i run the query it return only one run for each replies even though there are multiple replies for each status. 但是我的问题是,当我运行查询时,即使每个状态有多个答复,它也只为每个答复返回一次运行。 Honestly i dont know whether the problem is from the PHP loop or it is from the SQL queries it self. 老实说,我不知道问题是来自PHP循环还是来自SQL自身查询。 I have battling with this since two days now and have search other Stackoverflow question for help but still. 从两天以来,我一直在与这个问题作斗争,并且仍然在搜索其他Stackoverflow问题以寻求帮助。 I will be glad if anyone can help me. 如果有人可以帮助我,我将非常高兴。 Thank you. 谢谢。

User status are saved in the Status table and all replies on each status is saved in the replies table. 用户状态保存在“状态”表中,有关每个状态的所有答复都保存在“答复”表中。

These are my tables 这些是我的桌子

Users Table = gcm_users 用户表= gcm_users

   id
   name
   date

Status Table = gcm_status 状态表= gcm_status

   id
   userID
   status

Replies Table = gcm_status_replies 回复表= gcm_status_replies

   id
   statusID
   userID
   replies

This is my sql statement 这是我的SQL语句

$sqlSelect = ( 'SELECT gcm_status.status,
    gcm_status.id,
    gcm_status.userID,
    gcm_status_replies.id,
    gcm_status_replies.statusID,
    gcm_status_replies.userID,
    gcm_status_replies.message,
    gcm_users.id,
    gcm_users.name

    FROM gcm_users INNER JOIN gcm_status
    ON gcm_users.id = gcm_status.userID
    INNER JOIN gcm_status_replies
    ON gcm_status.id = gcm_status.id'
    array(''), $conn);

PHP Loops is PHP循环现为

       foreach ($sqlSelect as $row) {
             $respohnds = json_encode($row);
             echo $respohnds;
           }

I really need to help. 我真的需要帮助。

You're generating a separate json object for each row, and returning them one by one. 您将为每一行生成一个单独的json对象,然后将其逐个返回。

Maybe you should do this instead to return a single json object encoding your whole result set as an array. 也许您应该这样做,而不是返回一个将整个结果集编码为数组的json对象。

$respondhs = array();
foreach ($sqlSelect as $row) {
     $respohnds[] = json_encode($row);
}
echo $respohnds;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM