简体   繁体   中英

how to take specific value from sql array result and use it as a parameter in another sql statement in php

i need some help ,please. i have a sql statment that gives array of result

$query = "SELECT * FROM chat_messages WHERE sender_email='".$user_id."' OR receiver_email='".$user_id."' ORDER BY date_time DESC";

and its result like that

:[
    {"id":"47","sender_email":"amalkronz@hotmail.com","receiver_email":"test@test.com","message":"hee","date_time":"1456320044336"},
    {"id":"46","sender_email":"amalkronz@hotmail.com","receiver_email":"amalkronz@gmail.com","message":"hello","date_time":"1456319938285"},
    {"id":"45","sender_email":"amalkronz@hotmail.com","receiver_email":"amalkronz@gmail.com","message":"hi","date_time":"1456319929258"}
]

and i want to take the "sender_email" from each row and use it in another sql statement to get the user profile details and the same with the "receiver_email" how i can do that to get json response like that :

{ "success":true,"messages":[
      {"id":"156","sender_email":"sescooo@gmail.com","receiver_email":"jmaelagha@gmail.com","message":"hi","date_time":"1448660829608","sender":{"username":"test","img":"http:\/\/orchidatech.com\/sharearide\/images\/default_image.jpg","Gender":"","phone":"0049704197602","email":"sescooo@gmail.com","id":"101"},"receiver":{"username":"jmaagha agha","img":"https:\/\/lh5.googleusercontent.com\/-Ug34apPklis\/AAAAAAAAAAI\/AAAAAAAAAD0\/QUjGOKH0qsU\/photo.jpg?sz=50","Gender":"","phone":"0595114000","email":"jmaelagha@gmail.com","id":"98"}},
      {"id":"155","sender_email":"jma@gmail.com","receiver_email":"seo@gmail.com","message":"hi","date_time":"1448660780012","sender":{"username":"jmaagha agha","img":"https:photo.jpgsz=50","Gender":"","phone":"0595114000","email":"jma@gmail.com","id":"98"},"receiver":{"username":"test","img":"http:default_image.jpg","Gender":"","phone":"0049704197602","email":"seo@gmail.com","id":"101"}},
      {"id":"154","sender_email":"jma@gmail.com","receiver_email":"seo@gmail.com","message":"hi ahmed","date_time":"1448660747317","sender":{"username":"jma agha","img":"https:photo.jpg?sz=50","Gender":"","phone":"0595114000","email":"jma@gmail.com","id":"98"},"receiver":{"username":"test","img":"http:default_image.jpg","Gender":"","phone":"0049704197602","email":"seo@gmail.com","id":"101"}}
    ]
}

PS: the server doesn't support the mysqli fetch_all() thanks

聊天桌

用户表

You must loop over a query result that contains multiple rows. Take this example:

$mysqli->query("SELECT * FROM `sometable`"); //multiple rows returned
$savedValues = array();
while($nextRow = $mysqli->fetch_assoc()){
    $someValue = $nextRow['YourEmail_Column_name'];
    array_push($savedValues, $someValue);
}

$len = count($savedValues);
for($i = 0; $i < $len; $i++){
    $newQuery = "SELECT * FROM `someothertable` WHERE `email` = '$savedValues[$i]'";
    $mysqli->query($newQuery);
    $result = $mysqli->fetch_assoc(); //here is your row.  repeat above step for multiple rows!
}

Hope this helps you out!

PS: This could take up lots of memory in PHP, to solve this, perhaps use slimmer mysql statements based on conditions that you know will be true (or false).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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