dpg98445
2014-02-09 19:44
浏览 53
已采纳

无法从SQL查询中获取COUNT结果

When I run this code it only ever returns 0, even when I have the right information. I have checked in the database the same query and it returns a count of one when I have the correct info but this always returns 0.

$query = 'SELECT COUNT(*) FROM user WHERE UserName = "$username"  AND UserPass = "$password"';

    $result = mysqli_query($con, $query);

    $hello = mysqli_fetch_array($result); echo $hello[0]; exit;

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

当我运行此代码时,它只返回0,即使我有正确的信息。 我已经在数据库中检查了相同的查询,并且当我有正确的信息时它会返回一个计数但是它总是返回0.

  $ query ='SELECT COUNT(*  )FROM用户WHERE UserName =“$ username”AND UserPass =“$ password”'; 
 
 $ result = mysqli_query($ con,$ query); 
 
 $ hello = mysqli_fetch_array($ result);  echo $ hello [0]; 退出; 
   
 
  • 写回答
  • 好问题 提建议
  • 关注问题
  • 收藏
  • 邀请回答

2条回答 默认 最新

  • dongnigeng1295 2014-02-09 19:49
    已采纳

    change to

     $query = 'SELECT COUNT(*) FROM user WHERE UserName = "' . $username . '" AND UserPass = "'. $password .'"';
    
    已采纳该答案
    评论
    解决 无用
    打赏 举报
  • doucu5836 2014-02-09 19:58

    I really hope you left out your extensive security measures from your snippet. I suggest you apply some prepared statements pronto, but that's a different question!

    Instead of using a fetch, try using num_rows.

    $query = "SELECT 1 FROM `user` WHERE `UserName` = '$username'  AND `UserPass` = '$password'";
    $result = mysqli_query($con, $query);
    echo mysqli_num_rows($result);
    

    Try and play around with something like that!

    评论
    解决 无用
    打赏 举报

相关推荐 更多相似问题