Jump to content

Unread Message Function


imdead

Recommended Posts

Hey, im trying to create a little function that displays how many unread messages a user has

this is my function

function unread() {
$sql_pm_r = mysql_query("SELECT COUNT(*) FROM messages WHERE reciever=" . $user . " AND recieved='0'");
$num_rows = mysql_num_rows($sql_pm_r);
$sql_pm_check = mysql_query("SELECT id FROM messages WHERE reciever=" . $user . " AND recieved='0' LIMIT 1");
$num_new_pm = mysql_num_rows($sql_pm_check);
if ($num_new_pm > 0) {
$pms = '<a href="inbox.php" >('.$num_rows.')</a>';
} else {
$pms = '<a href="inbox.php" >(0)</a>';
}
echo $pms;
}

and i'm calling it on the index page as normal like

unread();

 

However not matter how many unread messages i have, i always get (0), this is how my sql table looks

id reciever sender subject message recieved

Link to comment
https://forums.phpfreaks.com/topic/249774-unread-message-function/
Share on other sites

$sql_pm_r will, assuming the query executed, will return a resource, not a number.

The first place to start is to see if your queries are in fact running as expected.

mysql_query("SELECT COUNT(*) FROM messages WHERE reciever=" . $user . " AND recieved='0'") or die(mysql_error());

Right just fixed a slight issue, $user was empty.

Working now :)

 

function unread() {
global $user;
mysql_query("SELECT COUNT(*) FROM messages WHERE reciever=" . $user . " AND recieved='0'") or die(mysql_error());
$num_rows = mysql_num_rows($sql_pm_r);
$sql_pm_check = mysql_query("SELECT id FROM messages WHERE reciever=" . $user . " AND recieved='0' LIMIT 1");
$num_new_pm = mysql_num_rows($sql_pm_check);
if ($num_new_pm > 0) {
$pms = '<a href="inbox.php" >('.$num_rows.')</a>';
} else {
$pms = '<a href="inbox.php" >(0)</a>';
}
echo $pms;
echo $sql_pm_r;
echo $user;
}

 

However i get

You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'USERNAME AND recieved='0'' at line 1

 

Which i fixed by changing

". $user ."

to

'". $user ."'

 

Now it's echo'ing

(0)USERNAME

 

Which means $sql_pm_r is empty right?

Have you checked for errors in your second query?

And echoing $sql_pm_r will be of no use to you..

 

Try this.

function unread() {
global $user;
$sql_pm_r = mysql_query("SELECT `id`, COUNT(*) as `unread` FROM `messages` WHERE reciever='" . $user . "' AND recieved='0' GROUP BY `reciever`") or die(mysql_error());
$rows = mysql_fetch_assoc($sql_pm_r);

if ($rows['unread'] > 0) {
	$pms = '<a href="inbox.php" >('.$rows['unread'].')</a>';
} else {
	$pms = '<a href="inbox.php" >(0)</a>';
}
}

Thorpe how do you mean?

 

and using that function didn't do anything.

 

at the moment i have this echo'ing

(1)Resource id #11USERNAME

 

with

function unread() {
global $user;
$sql_pm_r = mysql_query("SELECT COUNT(*) FROM messages WHERE reciever='". $user ."' AND recieved='0'") or die(mysql_error());
$num_rows = mysql_num_rows($sql_pm_r);
$sql_pm_check = mysql_query("SELECT id FROM messages WHERE reciever='" . $user . "' AND recieved='0' LIMIT 1") or die(mysql_error());
$num_new_pm = mysql_num_rows($sql_pm_check);
if ($num_new_pm > 0) {
$pms = '<a href="inbox.php" >('.$num_rows.')</a>';
} else {
$pms = '<a href="inbox.php" >(0)</a>';
}
echo $pms;
echo $sql_pm_r;
echo $user;
}

My function didnt do anything because I missed the echo statement.

I will explain your echos below:

(1) => This is because $num_rows is 1 and will always be 1 as you are using a COUNT(*);

Resource id #11 => This is because, as I already stated, $sql_pm_r contains a MySQL Resource, NOT actual results.

USERNAME => Pretty self explanatory.

 

thorpe is talking about your use of global $user;, instead of global you should pass in the variable into the function.

 

function unread($user) {
$sql_pm_r = mysql_query("SELECT `id`, COUNT(*) as `unread` FROM `messages` WHERE reciever='" . $user . "' AND recieved='0' GROUP BY `reciever`") or die(mysql_error());
$rows = mysql_fetch_assoc($sql_pm_r);

if ($rows['unread'] > 0) {
	$pms = '<a href="inbox.php" >('.$rows['unread'].')</a>';
} else {
	$pms = '<a href="inbox.php" >(0)</a>';
}
echo $pms;
}
// Then to call the function use:
unread($user);

Right got it working!

Cheers everyone

function unread($user) {
$sql_pm_r = mysql_query("SELECT id, COUNT(*) as unread FROM messages WHERE reciever='" . $user . "' AND recieved='0' GROUP BY reciever") or die(mysql_error());
$rows = mysql_fetch_assoc($sql_pm_r);
if ($rows['unread'] > 0) {
$pms = '<a href="inbox.php" >('.$rows['unread'].')</a>';
} else {
$pms = '<a href="inbox.php" >(0)</a>';
}
echo $pms;
}

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.