0

我正在尝试使用 PHP、Ajax、JS 向我的网站添加实时通知。它很好地请求 PHP 文件并将其加载到我的页面上,所以那里没有任何问题,但是 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>";
}
?>

如果没有返回任何行,则它会回显“”,但是有一个通知集供我的用户会话进行测试,并将其标记为未读。如果在页面本身上使用完全相同的查询,它会完美加载,所以根本不知道这里发生了什么。

4

1 回答 1

1

这一行应该是这样的:

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

或者

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

------------------已编辑---------------

请对此作出回应:

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

于 2013-04-27T23:36:25.077 回答