dtx3006 2013-04-27 23:31
浏览 35

PHP - 返回不正确的值

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.

  • 写回答

1条回答 默认 最新

  • dougao2830 2013-04-27 23:36
    关注

    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

    评论

报告相同问题?

悬赏问题

  • ¥15 微信公众平台自制会员卡可以通过收款码收款码收款进行自动积分吗
  • ¥15 随身WiFi网络灯亮但是没有网络,如何解决?
  • ¥15 gdf格式的脑电数据如何处理matlab
  • ¥20 重新写的代码替换了之后运行hbuliderx就这样了
  • ¥100 监控抖音用户作品更新可以微信公众号提醒
  • ¥15 UE5 如何可以不渲染HDRIBackdrop背景
  • ¥70 2048小游戏毕设项目
  • ¥20 mysql架构,按照姓名分表
  • ¥15 MATLAB实现区间[a,b]上的Gauss-Legendre积分
  • ¥15 delphi webbrowser组件网页下拉菜单自动选择问题