2013-07-11 114 views
0
不工作

基本上我有2個表LEFT JOIN 2個表

  • 主題
  • 用戶

我想使用左連接,這樣我可以鏈接 「posted_by」在「用戶」中用「user_id」在「主題」中顯示,以便我可以輸出顯示的users.username以及users.profile(頭像圖片)。

這是我目前的代碼,這是給我布爾錯誤。

 <?php 
    include 'core/init.php'; 
    include 'includes/overall/header.php'; 

    $sql = " SELECT *, users.id, users.username, users.profile 
     FROM `topics` 
     LEFT JOIN 
     users ON topics.posted_by = " . mysql_real_escape_string($_GET['topic_id']) . " users.user_id ORDER BY `posted` DESC"; 

    $result = mysql_query($sql); 

    // Start looping table row 
    while($rows = mysql_fetch_array($result)){ 
    ?> 
    <table> 
     <tr> 
     <td rowspan="4"> Avatar code to go here<br> 
      <? echo $rows['username']; ?></td> 
     <td><? echo $rows['category']; ?> > <? echo $rows['sub_category']; ?> </td> 
     </tr> 
     <tr> 
     <td><? echo $rows['posted']; ?></td> 
     </tr> 
     <tr> 
     <td><? echo $rows['topic_data']; ?></td> 
     </tr> 
     <tr> 
     <td><a href="view_topic.php?id=<? echo $rows['topic_id']; ?>">Reply</a> (<? echo $rows['reply']; ?>) Replies</td> 
     </tr> 
    </table> 

    <?php 
    // Exit looping and close connection 
    } 
    mysql_close(); 
    ?> 
+1

如果你可以給出實際的錯誤信息,這將是有幫助的。雖然我會猜測你的查詢失敗 - 嘗試添加對「mysql_error」的調用,以查看數據庫實際返回的內容。 – andrewsi

+0

你的查詢是錯誤的。這個問題會更好,如果你刪除了所有的HTML代碼,向我們展示你的錯誤,並重點修復查詢 – Cfreak

+0

我得到的錯誤是 警告:mysql_fetch_array()期望參數1是資源,布爾在/ home3 /第15行的網站/ public_html/topics.php 第15行是while($ rows = mysql_fetch_array($ result)){ – user2571547

回答

0

我相信你使用的是像

$sql = "SELECT users.user_id, users.username, users.profile, topics.topic_id,  topics.category, topics.sub_category, 
topics.topic_data, topics.posted_by, topics.posted, topics.view, topics.reply 
FROM users WHERE topics.posted_by = users.user_id ORDER BY topics.posted DESC"; 

嘗試增加

$ SQL的東西=「SELECT用戶。 user_id,users.username,users.profile,topics.topic_id,topics.category,topics.sub_category, topics.topic_data,topics.posted_by,topics.posted,topics.view,topi cs.reply 從用戶,主題 WHERE topics.posted_by = users.user_id ORDER BY topics.posted DESC「;

+0

這樣做了! FROM> _ < – user2571547

0

ON topics.posted_by = " . mysql_real_escape_string($_GET['topic_id']) . " users.user_id你知道這會產生ON topics.posted_by = 1 users.user_id例如這是無效的SQL語法。使用WHERE強烈不推薦,而不是

ON topics.posted_by = users.user_id WHERE topics.id = (topic_id_variable)

P.S:使用mysql_。你應該改變API。

+0

$ sql =「SELECT topics.topic_id,topics.category,topics.sub_category,topics.topic_data, topics.posted_by,topics.posted,users.id,users.username,users.profile FROM'topics' LEFT JOIN users WHERE topics.posted_by = users.user_id ORDER BY'posted' DESC「; 更好嗎? – user2571547

+0

不幸的是,沒有。使用'JOIN'後,你必須提供'ON'或'USING',然後在WHERE子句中按topics.id過濾。 –

+0

'SELECT topics.topic_id,topics.category,topics.sub_category,topics.topic_data,topics.posted_by,topics.posted,users.id,users.username,users.profile FROM topics LEFT JOIN users ON topics。posted_by = users.user_id ORDER BY posted DESC' or 'SELECT topics.topic_id,topics.category,topics.sub_category,topics.topic_data,topics.posted_by,topics.posted,users.id,users.username,users .profile FROM topics LEFT JOIN users ON topic.posted_by = users.user_id WHERE topics.id ='「。mysql_real_escape_string($ _ GET ['topic_id'])。''ORDER BY posted DESC' –

0

您需要提供users在您的連接條款中與topics的關係。你已經爲你的連接子句提供了一個外部變量。這不允許數據庫引擎建立兩個表應該如何連接的關係。

我相信你期待的查詢應該是這樣的

$sql = " SELECT *, users.id, users.username, users.profile 
     FROM `topics` 
     LEFT JOIN 
     users ON topics.posted_by = users.user_id 
     WHERE topics.id = '" . mysql_real_escape_string($_GET['topic_id']) . "' 
     ORDER BY `posted` DESC"; 
+0

我以爲我有? $ sql =「SELECT topics.topic_id,topics .category,topics.sub_category,topics.topic_data, topics.posted_by,topics.posted,users.id,users.username,users.profile FROM'topics' LEFT JOIN users WHERE topics.posted_by = users.user_id ORDER BY'posted' DESC「; posted_by(是一個init)與user_id相關聯,我是否做錯了? – user2571547

+0

你的代碼並不能反映你剛纔所說的查詢 – DevZer0