基本上我有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();
?>
如果你可以給出實際的錯誤信息,這將是有幫助的。雖然我會猜測你的查詢失敗 - 嘗試添加對「mysql_error」的調用,以查看數據庫實際返回的內容。 – andrewsi
你的查詢是錯誤的。這個問題會更好,如果你刪除了所有的HTML代碼,向我們展示你的錯誤,並重點修復查詢 – Cfreak
我得到的錯誤是 警告:mysql_fetch_array()期望參數1是資源,布爾在/ home3 /第15行的網站/ public_html/topics.php 第15行是while($ rows = mysql_fetch_array($ result)){ – user2571547