Click here to Skip to main content
15,886,678 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,

Im trying to get my messages link to show if there is a new message to the user that is currently signed in to the session.

In the messages database table, 0 is unread and 1 is read so any messages that are unread and sent to the session username should be returned. Currently, there are no rows being returned (because all messages are read (or 1)) so the link should not show the '(new)' part but it does. I changed the count($success) to > 1 and the code in the else was executed.

Here is my code:

PHP
$s_username = $_SESSION['username'];

$db = Database::getConnection();
				$query = 'SELECT * FROM messages WHERE messageTo = :to AND messageRead = 0';
				$output = $db->prepare($query);
				$output->bindParam(':to', $s_username);
				$success = $output->execute();
					if (count($success) > 0){
						$msg = '<li><a href="messages.php">My Messages (new)</a></li>';
					} else {
						$msg = '<li><a href="messages.php">My Messages</a></li>';
					}
				echo $msg;


Am I missing something obvious? I understand that if there are no rows then it does = 0 because its empty, but if it is greater than and there are unread messages to the session user, it should work, right?
Posted
Comments
ramyajaya 7-Apr-15 18:25pm    
Your logic is correct. Check if the query executes manually and the query formed is correct.

1 solution

This:
PHP
$success = $output->execute();

only returns true or false, indicating the query execution was successful or not - it does not return the amount of records returned.
See: http://php.net/manual/en/pdostatement.execute.php[^]

To get the amount of records returned, use rowCount():
http://php.net/manual/en/pdostatement.rowcount.php[^]

But if you are only interested in the amount of records that fit your query-predicate (and not in the records themselves), you shouldn't SELECT * but SELECT Count(*) , then fetch the query result like you already do in other parts of your code, with fetch() or fetchAll() and then read from the returned array:
http://php.net/manual/en/pdostatement.fetchall.php[^]

Edit: formatting
 
Share this answer
 
v2
Comments
jba1991 8-Apr-15 9:13am    
I see! So because it returned true or false it means there was only 1 record which is why the count was saying there was > 0 records, even though (i thought) there wasnt!

Does that mean something like this should work:

$success = $output->execute();
if ($success){
$results = $output->fetchAll();
if (count($results) > 0){
$msg = '<li>My Messages (new)</li>';
} else {
$msg = '<li>My Messages</li>';
}
}
Sascha Lefèvre 8-Apr-15 9:18am    
Yes, that should work
jba1991 9-Apr-15 7:41am    
Its doing the same as before :(
Sascha Lefèvre 9-Apr-15 8:20am    
You mean it always displays "(new)" ?
jba1991 9-Apr-15 10:43am    
Yeah. Unless I change it to > 1 instead of > 0. And even if there are records that should be returned, when using > 1, it doesnt show as (new).

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900