hksfho hksfho - 3 years ago 152
JSON Question

how to select mysql in multi table and mix in one json?

This is my first table

SELECT * FROM tb_chat WHERE senderid = "1"


output:

{
"id":"5",
"itemid":"43",
"senderid":"1",
"receiverid":"11",
"sendtime":"2015-10-04 23:31:35",
"sendcontent":"1232342sdfsasg",
"remark":null
}


This is my second table

SELECT * FROM `tb_user` WHERE id = "1"


output

{
"id":"1",
"username":"hksfho",
"password":"ae6e16c74efb51b616b0d4f7151aff88",
"email":"ho@hksf.hk",
"installationId":"E705A03522F3E85768581A7222AC40F6"
}


I want to mix two tables in to one json using php
the tb_chat.senderid = tb_user.userid

enter image description here

Answer Source

NO Code so a logical answer is provided , do your stuff as below-

$result =mysqli_fetch_assoc(mysqli_query(SELECT * FROM tb_chat WHERE senderid = "1"));
$result2 =mysqli_fetch_assoc(mysqli_query(SELECT * FROM  `tb_user` WHERE id =  "1"));
$result["senderid"]=$result2;
echo json_encode($result);
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download