2013-12-19 24 views
0

嘗試從另一個表中回顯作者列。目前數據庫中有兩個表,一個叫做entries,另一個叫comments。兩者都有ID,但都具有相同的列名「作者」。在SQL語句中,你會做select * from $table1 t1, $table2 t2 where t1.blogID = t2.blogID and t1.blogID = $postID。我知道如何使用fetch_object()從MySQL的另一個相關表顯示行使用fetch_object()

if (!empty($postID)) { 
$command = "select * from $table_name where blogID = $postID"; 
$result = $db->query($command); 
while ($data = $result->fetch_object()) { 
    $postID = $data->blogID; 
    echo "<TR><TD>".$postID."</TD>"; 
    echo "<TD>".$data->author."</TD>"; 
    echo "<TD>".$data->date."</TD>"; 
    echo "<TD>".$data->entry."</TD></TR>\n"; 

只是一個表呼應但是,那麼,如果我想$command到兩個表中選擇,我將如何迴應讓說t1.author?我的意思是$data->t1.author?那有意義嗎?或者你們建議使用我在上面設置的格式?

+0

你需要閱讀本:http://en.wikipedia.org/wiki/Join_%28SQL%29 –

+0

@MarcB感謝您的建議,但是這不是有益的。 – mythoslife

+0

然後''var_dump($ data)'可能會告訴你你需要做什麼,一旦你完成了連接查詢。 –

回答

0
$command="select t1.name as t1name , t2.name as t2name from t1tabla as t1 ,t2table as t2"; 
$result = $db->query($command); 
while ($data = $result->fetch_object()) { 

echo $data->t1name .'<br>'; 
echo $data->t2name.'<br>' 
} 
+0

這可能工作@Rahi – mythoslife

+0

不起作用@Rahi – mythoslife

+0

它將工作@mythoslife。也許你做錯了什麼。這是如何工作的。請檢查你的query.print_r($ result)以檢查它給出了什麼。 – Rahi

相關問題