简体   繁体   English

PHP-返回不正确的值

[英]PHP - Returning incorrect value

I am trying to add real-time notifications to my website using PHP, Ajax, JS. 我正在尝试使用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. 它要求PHP文件正常并将其加载到我的页面上,所以那里没有任何问题,但是PHP返回的行数不正确,并且文件中似乎没有任何问题,所以我真的很困惑。

Here is my PHP: 这是我的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. 如果在页面本身上使用完全相同的查询,它将完美地加载,因此根本不知道发生了什么。

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 PS:不要使用mysql扩展名...去mysqli或PDO ...您的代码容易受到sql注入的攻击

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM