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 使用EMD去噪处理RML2016数据集时候的原理
  • ¥15 神经网络预测均方误差很小 但是图像上看着差别太大
  • ¥15 Oracle中如何从clob类型截取特定字符串后面的字符
  • ¥15 想通过pywinauto自动电机应用程序按钮,但是找不到应用程序按钮信息
  • ¥15 如何在炒股软件中,爬到我想看的日k线
  • ¥15 seatunnel 怎么配置Elasticsearch
  • ¥15 PSCAD安装问题 ERROR: Visual Studio 2013, 2015, 2017 or 2019 is not found in the system.
  • ¥15 (标签-MATLAB|关键词-多址)
  • ¥15 关于#MATLAB#的问题,如何解决?(相关搜索:信噪比,系统容量)
  • ¥500 52810做蓝牙接受端