2013-03-08 74 views
0

我的問題是下面的代碼註釋&數組的評分區域應該有一個註釋,因爲DB歷史表的確是...?SQL左連接部分數組是空的

SELECT users.uid, users.username, history.user_id, history.comment, history.rating 
FROM history 
LEFT JOIN users 
ON users.uid=history.user_id 
WHERE history.book_id="$bid" 

它返回:

Array ([uid] => 3 [username] => Reign [user_id] => 3 [comment] => [rating] =>) 
+1

[**請不要在新代碼中使用'mysql_ *'函數**](http://bit.ly/phpmsql)。他們不再被維護[並且被正式棄用](http://j.mp/XqV7Lp)。看到[**紅框**](http://j.mp/Te9zIL)?學習[*準備的語句*](http://j.mp/T9hLWi),並使用[PDO](http://php.net/pdo)或[MySQLi](http://php.net/ mysqli) - [這篇文章](http://j.mp/QEx8IB)將幫助你決定哪個。 – Kermit 2013-03-08 20:59:27

+0

它顯然會返回歷史記錄,因爲history.user_id在結果中爲3。你確定有關於該特定記錄的評論和評分? – 2013-03-08 21:02:15

+2

不應該這樣做'=「$ bid」'。這是一個SQL注入錯誤。應改爲'=:bid'。 – Luke 2013-03-08 21:07:08

回答

4

然後你想使用INNER JOIN。 A LEFT JOIN將返回NULL值。

SELECT users.uid, users.username, history.user_id, history.comment, history.rating 
FROM history 
INNER JOIN users 
ON users.uid=history.user_id 
WHERE history.book_id="$bid"