嘗試從另一個表中回顯作者列。目前數據庫中有兩個表,一個叫做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
?那有意義嗎?或者你們建議使用我在上面設置的格式?
你需要閱讀本:http://en.wikipedia.org/wiki/Join_%28SQL%29 –
@MarcB感謝您的建議,但是這不是有益的。 – mythoslife
然後''var_dump($ data)'可能會告訴你你需要做什麼,一旦你完成了連接查詢。 –