2015-10-05 50 views
0

這是我的第一個表如何在多表中選擇mysql並混合到一個json中?

SELECT * FROM tb_chat WHERE senderid = "1" 

輸出:

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

這是我的第二個表

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

輸出

{ 
    "id":"1", 
    "username":"hksfho", 
    "password":"ae6e16c74efb51b616b0d4f7151aff88", 
    "email":"[email protected]", 
    "installationId":"E705A03522F3E85768581A7222AC40F6" 
} 

我想混2噸ABLES在一個JSON使用PHP 的tb_chat.senderid = tb_user.userid

enter image description here

+3

向我們展示你的努力,你有什麼到目前爲止已經試過?爲此,您必須逐個迭代第二個查詢並將其與其父級合併。或者如果您使用任何'ORM',這很容易實現。 – Manwal

+0

@MahaDev我想輸出的圖片 – hksfho

+0

$ final_result = new array(); $ final_result = $ query1_result; $ final_result ['senderid'] = $ query2_result; json_encode($ final_result); –

回答

1

沒有代碼,從而提供了一個合乎邏輯的答案,你做的東西,如下─

$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); 
+0

真的這樣做了嗎? –

+0

我希望如此約翰.. –

0

所以,如果你得到的查詢結果中json格式,你可以把它變成數組,合併它們,使之以json格式再次。 它有很大的開銷:

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

所以,我覺得你should得到查詢結果中array,ID ORM取決於你用什麼,什麼,如何做到這一點的。

相關問題