dongling5411
2015-06-27 04:51
浏览 370
已采纳

mysql_query返回错误的结果

Consider the following code

if ( isset( $_SESSION['FBID'] )   ) {
    $uid      = $_SESSION['FBID'];
    $sql      = "SELECT *, count(member_nr) AS notifyMe 
                 FROM poolWinners 
                 WHERE member_nr = '$uid'   AND notification ='1'";
    $result = mysql_query($sql);
    while($row=mysql_fetch_array($result)){
       $notification = $row['notifyMe'];
    }//while
      if ( $notification > 0 ) {
        echo '<span class="badge">' . $notification . '</span>';
    } //if
    var_dump($notification);
} //isset( $_SESSION['FBID'] )

The above script returns how many notifications a member has as you can see in image below enter image description here

My Problem

The script is returning the wrong result (wrong number of notifications). Have a look at the table below, the member number appears 3 times in the table so: $notification = $row['notifyMe'] Should = 3 AND NOT 1

What am I missing or doing wrong here? Thanks for reading

图片转代码服务由CSDN问答提供 功能建议

考虑以下代码

  if(isset($ _SESSION)  ['FBID'])){
 $ uid = $ _SESSION ['FBID']; 
 $ sql =“SELECT *,count(member_nr)AS notifyMe 
 FROM poolWinners 
 WHERE member_nr ='$ uid'AND  notification ='1'“; 
 $ result = mysql_query($ sql); 
 while($ row = mysql_fetch_array($ result)){
 $ notification = $ row ['notifyMe']; 
} //  while 
 if($ notification&gt; 0){
 echo'&lt; span class =“badge”&gt;'  。  $通知。  '&lt; / span&gt;'; 
} // if 
 var_dump($ notification); 
} // isset($ _SESSION ['FBID'])
   
 \  n 

上面的脚本返回成员拥有的通知数,如下图所示:

我的问题

脚本返回错误的结果(通知数量错误)。 看看下面的表格,会员编号在表格中出现3次,所以: $ notification = $ row ['notifyMe'] 应该= 3而不是1

我在这里错过了什么或做错了什么? 感谢您阅读

  • 写回答
  • 好问题 提建议
  • 关注问题
  • 收藏
  • 邀请回答

5条回答 默认 最新

  • dosrmo0442 2015-06-27 04:53
    已采纳

    Use

    $sql      = "SELECT *, count(*) AS notifyMe 
                 FROM poolWinners 
                 WHERE member_nr = '$uid'   AND notification ='1'";
    

    Notice count(*) , it will fetch how many records are matching criteria.

    And initialize $notification = 0; at the start.

    已采纳该答案
    评论
    解决 无用
    打赏 举报
  • doumeng3080 2015-06-27 05:20

    Have you tried approaching it from this angle

    $sql = "SELECT * FROM poolWinners WHERE member_nr = '$uid'   AND notification ='1'";
    
    $result = mysql_query($sql);
    $notification = array();
        while($row=mysql_fetch_array($result)){
           $notification[] = $row['notifyMe'];
        }
    //an array count on notification should give you the number of elements in the array i.e those that matched the query
    
    $total_count = count($notification);
    
    评论
    解决 无用
    打赏 举报
  • dtpw54085 2015-06-27 05:34

    In your code the notification will be always one, since it will take only the notifyMe field of last row in the result set.

    If you want to get number of notifications, try this

    if ( isset( $_SESSION['FBID'] )   ) {
        $uid      = $_SESSION['FBID'];
        $sql      = "SELECT *, count(member_nr) AS notifyMe 
                     FROM poolWinners 
                     WHERE member_nr = '$uid'   AND notification ='1'";
        $result = mysql_query($sql);
        $notification = 0;
        while($row=mysql_fetch_array($result)){
           $notification++; 
           /*
           OR $notification += $row['notifyMe'];
           */
        }//while
          if ( $notification > 0 ) {
            echo '<span class="badge">' . $notification . '</span>';
        } //if
        var_dump($notification);
    } //isset( $_SESSION['FBID'] )
    
    评论
    解决 无用
    打赏 举报
  • dongtaochan0777 2015-06-27 05:45
    $sql      = "SELECT *  
                 FROM poolWinners 
                 WHERE member_nr = '$uid'   AND notification ='1'";
    
    $result = mysql_query($sql);
    $notification = mysql_num_rows($result);
    
    评论
    解决 无用
    打赏 举报
  • duanhong1985 2015-06-27 05:46
    $sql = "SELECT * FROM poolWinners WHERE member_nr = '$uid'   AND notification ='1'";
    
    if ($result=mysqli_query($con,$sql))
      {
      // Return the number of rows in result set
    echo "Toal notification".mysqli_num_rows($result);
      }
    
    mysqli_close($con);
    ?> 
    
    评论
    解决 无用
    打赏 举报

相关推荐 更多相似问题