I am trying to add real-time notifications to my website using PHP, Ajax, JS. It requests the PHP file fine and loads it onto my page so there is nothing wrong there, however the PHP is returning an incorrect number of rows and there doesn't seem to be any problems in the file so I am really confused.
Here is my PHP:
<?php
include("config.php");
$query = "SELECT * FROM notifications WHERE user = '$myUser' AND isread = '0'";
$result = mysql_query($query);
$num = mysql_num_rows($result);
if($num == 0){
print "";
} else if($num !== 0){
print "<span class='notification'> " . $num . " </span>";
}
?>
If there aren't any rows returned, then it echoes "", however there is a notification set for my user session to test and it is marked as unread. It loads perfectly with the exact same query if used on the page itself, so don't know what's going on at all here.
This line should be like:
else{
print "<span class='notification'> " . $nNum . " </span>";
}
?>
Or
else if($num != 0){
print "<span class='notification'> " . $num . " </span>";
}
?>
------------------EDITED---------------------------
Please do an echo to this:
<?php
include("config.php");
$query = "SELECT * FROM notifications WHERE user = '$myUser' AND isread = '0'";
$result = mysql_query($query);
$num = mysql_num_rows($result);
echo $query; //THIS and copy that and put this directly on your mysql manager
if($num == 0){
print "";
} else if($num != 0){
print "<span class='notification'> " . $num . " </span>";
}
?>
PS: Dont use mysql extension...go for mysqli or PDO...your code is vulnerable to sql injection