0

I am making a simple friends feature for my site and when people get a friend request I want it to show on there friends panel link so I tried this.

 $q = $dbc -> prepare("SELECT id_to read FROM friend_requests WHERE id = ? && read == '0'");
 $q -> execute(array($details['id']));
 $request_count = $q -> rowCount();
 if ($request_count > 0) {
echo '<li><a href="/friends"><img src="/gameimages/friends.png" height="16" width="16" alt="" /><strong>Friends (' . $request_count . ')</strong></a></li>';
 }
 else {
echo $request_count;
echo '<li><a href="/friends"><img src="/gameimages/friends.png" height="16" width="16" alt="" />Friends </a></li>';
 }

Now on the friends page where requests are shown all requests get the column of read set to 1, and by default it is 0. If there are rows with 0 it shall be a new request and the query above should fire letting users know that they have new requests, what am I doing wrong?

3 Answers 3

3

The problem is your query:

SELECT id_to read FROM friend_requests WHERE id = ? && read == '0'

should be

SELECT id_to, read FROM friend_requests WHERE id = ? and read = '0'
Sign up to request clarification or add additional context in comments.

Comments

1

Read is a reserved word in MySQL and need to be escaped with backticks:

SELECT id_to, `read` FROM friend_requests WHERE id = ? AND `read` = '0';

2 Comments

Ah ok even when I did the above it still did not work, and I have always used && in my mysql statements, what is this reserved for, do you know, and thanks :)
Some words need to be reserved for the SQL to be properly interpreted. SELECT select ... is rather ambiguous don't you think? Read documentation for further information: dev.mysql.com/doc/refman/5.5/en/reserved-words.html
1

&& read == '0'

Did you mean

 AND read = 0

?

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.