drcmue4619 2016-07-26 10:58
浏览 22
已采纳

Mysqli - 平等和不同的表达

I'm having some problems with my query that is always returning false, but it should return true if not exists. The problem is since the expression doesn't exists, it turns false false.

function isRegistered($mysqli, $username, $sitename, $status){
if($stmt = $mysqli->prepare("SELECT COUNT(*) FROM table1 WHERE username= ? AND sitename = ? AND status != ?")){
   $stmt->bind_param("sss", $username, $sitename, $status);
    $stmt->execute();
    $stmt->store_result();
        if($stmt->num_rows == 0){
            return true;
        }else{
            return false;
        }
}

}

I'm after that seeking if the function is true or false. If it's false, it trhows an error, if it's true, it follows for the next step. The problem is that always coming false (even if thats not true).

Basically the user only can register once in a specific area. So the first time it should be allowed, and the second time it doesnt. Could someone check what's going wrong with my query? Thanks

  • 写回答

1条回答 默认 最新

  • douri4459 2016-07-26 11:00
    关注

    The number of rows for an aggregation query is always 1. If nothing matches the WHERE clause, then one row will be returned with the value of "1".

    You can fix this either by changing the SELECT COUNT(*) to something non-aggregated, such as SELECT 1 or SELECT username. Or, you can read the value of COUNT(*) back from the query and use that.

    I'll let you choose how to proceed.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥16 Qphython 用xlrd读取excel报错
  • ¥15 单片机学习顺序问题!!
  • ¥15 ikuai客户端多拨vpn,重启总是有个别重拨不上
  • ¥20 关于#anlogic#sdram#的问题,如何解决?(关键词-performance)
  • ¥15 相敏解调 matlab
  • ¥15 求lingo代码和思路
  • ¥15 公交车和无人机协同运输
  • ¥15 stm32代码移植没反应
  • ¥15 matlab基于pde算法图像修复,为什么只能对示例图像有效
  • ¥100 连续两帧图像高速减法