hksfho
hksfho

Reputation: 114

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":"[email protected]",
    "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

Upvotes: 0

Views: 481

Answers (2)

Rohit Kumar
Rohit Kumar

Reputation: 1958

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);

Upvotes: 2

sergio
sergio

Reputation: 5260

So, if you get the result of query in json format, you can turn it into arrays, merge them and make it to json format again. BUT it has big overhead:

json_encode(array_merge(json_decode($firstResult, true),json_decode($secondResult, true)))

So, I think you should get the result of query in array, id depends what ORM you use, of whatever, how to do this.

Upvotes: 0

Related Questions