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.

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

报告相同问题?

悬赏问题

  • ¥15 做个有关计算的小程序
  • ¥15 MPI读取tif文件无法正常给各进程分配路径
  • ¥15 如何用MATLAB实现以下三个公式(有相互嵌套)
  • ¥30 关于#算法#的问题:运用EViews第九版本进行一系列计量经济学的时间数列数据回归分析预测问题 求各位帮我解答一下
  • ¥15 setInterval 页面闪烁,怎么解决
  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化