0

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'>&nbsp;&nbsp;" . $num . "&nbsp;&nbsp;</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.

4
  • I forgot to change that when I posted the code here, even if that is undefined, it returns the empty print from the if statement that is shown for 0 rows returned. Commented Apr 27, 2013 at 23:36
  • If you are about to start real coding you should be at least be aware of the if statement. Why else if and not else? Commented Apr 27, 2013 at 23:38
  • This probably has been said at least once everyday for like the past 5 years even before SO existed, but PHP news travel very slow apparently. mysql is old, about the dissapear, don't use it. Check any of the functions in the offical docs, there are warnings everywhere. php.net/manual/en/book.mysql.php Commented Apr 27, 2013 at 23:39
  • I already know that, but thanks elclanrs + hek2mgl, I am using if for certain reasons related to how my site works. Commented Apr 27, 2013 at 23:41

1 Answer 1

1

This line should be like:

else{
 print "<span class='notification'>&nbsp;&nbsp;" . $nNum . "&nbsp;&nbsp;</span>";
}
?>

Or

else if($num != 0){
  print "<span class='notification'>&nbsp;&nbsp;" . $num . "&nbsp;&nbsp;</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'>&nbsp;&nbsp;" . $num . "&nbsp;&nbsp;</span>";
}
?>

PS: Dont use mysql extension...go for mysqli or PDO...your code is vulnerable to sql injection

Sign up to request clarification or add additional context in comments.

6 Comments

That wouldn't make a difference, it still shows the first print, if I type "Error" for example in it, that is what is returned on my page.
That still wouldn't change anything, this is the second print which you are editing, it doesn't even get to that, the query is returning 0 rows although there is a row that should be returned.
Sorry if I seemed rude, I am grateful for you trying to help, don't get me wrong. Just as I said it's the first print, so the second one is completely unnecessary to edit.
Try with that...echo $query and put that info on you mysql manager...i use heidi...its free....see if that query returns something
Found the problem, I should I have debugged more before I asked the question here I guess, echoing the query did it though, it wasn't picking up my user variable so it didn't actually query that, pretty stupid problem :/ thanks for the help!
|

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.