0

My problem is the following code the comment&rating area of the array should have a comment in it as the DB history table does ...?

 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"

It returns :

 Array ( [uid] => 3 [username] => Reign [user_id] => 3 [comment] => [rating] => )
3
  • 1
    Please, don't use mysql_* functions in new code. They are no longer maintained and are officially deprecated. See the red box? Learn about prepared statements instead, and use PDO or MySQLi - this article will help you decide which. Commented Mar 8, 2013 at 20:59
  • it is obviously returning a history record, since history.user_id is 3 in the result. are you sure there's a comment and rating set on that particular record? Commented Mar 8, 2013 at 21:02
  • 2
    Shouldn't do ="$bid" either. That's a SQL injection mistake. Should change to =:bid. Commented Mar 8, 2013 at 21:07

1 Answer 1

4

Then you want to use an INNER JOIN. A LEFT JOIN will return NULL values.

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"
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.