2013-03-08 78 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"