drhzc64482 2016-08-05 03:42
浏览 50
已采纳

get_result在应该返回false时不返回false

The query is executing normally for a successful database search, but for an unsuccessful search it is still following the successful execution path. Is there a different way to analyze $result for an unsuccessful Mysql database query?

$query = "SELECT password
                FROM consumer
                WHERE username = ? ";

            //prepare query
        if ($stmt = $this->conn->prepare($query)) {
            $stmt->bind_param('s', $username);
            $stmt->execute();


            $result = $stmt->get_result();

            if (!is_null($result) && empty($stmt->last_error) && count($result)>0) {

               //successful query, going to this every time even when there
               //is no row in the database that matches the query

            } else {

               //unsuccessful query 
            }
  • 写回答

1条回答 默认 最新

  • dongwai4434 2016-08-05 03:52
    关注

    Using count() almost gets you there, except for one small problem. Even if there is no matching password, a single result is still returned, hence count will not return zero. Instead, you can try using mysqli_num_rows, which returns the number of records affected by query. In the case of an empty result set, this should return 0, hence the following check should work:

    $rowcount = mysqli_num_rows($result);
    if ($rowcount == 0) {
        // unsuccessful query
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥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做蓝牙接受端
  • ¥15 基于PLC的三轴机械手程序
  • ¥15 多址通信方式的抗噪声性能和系统容量对比
  • ¥15 msix packaging tool打包问题
  • ¥15 finalshell节点的搭建代码和那个端口代码教程
  • ¥15 Centos / PETSc / PETGEM