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

问题描述 投票:0回答:1
$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
    )
);    

我想传入单个数组。

喜欢:

"user_suggested_data": {
    "0": {
        "user_id": "121",
        "full_name": "Ankit Shah",
        "username": "shah_ankit39",
        "profile_picture": null,
        "status": 0 
    },
    "1": {
        "user_id": "122",
        "full_name": "pooja",
        "username": "pooja25",
        "profile_picture": null,
        "status": 0  
    },
    "2": {
        "user_id": "123",
        "full_name": "swapnil",
        "username": "swapnil25",
        "profile_picture": null,
        "status": 0 
    },       
}
php mysql arrays mysqli
1个回答
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);
© www.soinside.com 2019 - 2024. All rights reserved.