如何从单个数组中的两个查询传递json

时间:2018-01-25 07:10:42

标签: mysql arrays database

$ sql =“SELECT user_registration.user_id,user_registration.full_name,user_registration.username,user_profile.profile_picture FROM user_registration LEFT JOIN user_profile ON user_registration.user_id = user_profile.user_id”;

    $result = $con->query($sql);

    if ($result->num_rows > 0) {

    while($row = $result->fetch_assoc()) {
            $outaa[] = $row;

            $f_user_id = $row['user_id'];

        $sql1 = "SELECT status FROM user_follower WHERE (from_user_id = '$f_user_id' AND to_user_id = '$user_id' ) OR (from_user_id = '$user_id' AND to_user_id = '$f_user_id' )";

        $result1 = $con->query($sql1);
        if ($result1->num_rows > 0) {
        while($row1 = $result1->fetch_assoc()) {

            $outaa[] = $row1;
        } 
        }
        else {
            $outaa[] =  "No";
        }   

    }
    }
   $out = array_merge(array('result'=>'true','reason'=>'Data Fetching Succesfully','user_suggested_data' => $outaa));     

我想传递单个数组。

Ans喜欢:

“user_suggested_data”:{         “0”:{             “user_id”:“121”,             “full_name”:“Ankit Shah”,             “username”:“shah_ankit39”,             “profile_picture”:null,             “状态”:0         },         “1”:{             “user_id”:“122”,             “full_name”:“pooja”,             “username”:“pooja25”,             “profile_picture”:null,             “状态”:0
        },         “2”:{             “user_id”:“123”,             “full_name”:“swapnil”,             “username”:“swapnil25”,             “profile_picture”:null,             “状态”:0         },

}

1 个答案:

答案 0 :(得分:1)

您可以通过将SQL查询改进为:

来实现此目的
  SELECT
    user_registration.user_id,
    user_registration.full_name,
    user_registration.username,
    user_profile.profile_picture,
    user_follower.status
  FROM
    user_registration LEFT JOIN user_profile USING(user_id)
  WHERE
    (user_follower.to_user_id = '$user_id'
      AND user_follower.from_user_id = user_registration.user_id)
    OR (user_follower.from_user_id = '$user_id'
      AND user_follower.to_user_id = user_registration.user_id)"

将其存储为$sql变量中的字符串,最后执行查询:

$result = $con->query($sql);