预准备语句 php 的打印结果(mysqli::num_rows 设置不正确)


Print result of prepared statement php (mysqli::num_rows not being set correctly)

>我最近从普通语句更新

$result = mysql_query("SELECT user_id from users WHERE user_id = '$user_id'");

到预处理语句(安全性)

prepare("SELECT user_id FROM users WHERE user_id = ?");

我遵循了一些教程,但这仍然不起作用:

public function isUserRegistered($user_id) {
        print $user_id;
        $stmt = $this->conn->prepare("SELECT user_id FROM users WHERE user_id = ?");
        $stmt->bind_param("s", $user_id);
        if ($stmt->execute()) {
            $stmt->bind_result($user_id);
            $stmt->fetch();     
            $no_of_rows = $stmt->num_rows;
            print $no_of_rows;
            $stmt -> close();
            if ($no_of_rows > 0) {
                print "Finally";
                // user already registered
                return true;
            } else {
                print "Stupid";
                // user is not registered
                return false;
            }
        }
    }

提供的 id 存在,因为我可以在正在打印的控制台中看到它。if ($stmt->execute())正在执行,但由于某种原因没有任何结果。

我该如何解决这个问题以及如何打印结果?

我也试过:

while ($stmt->fetch()) {
        printf ("%s (%s)'n", $user_id);
    }

问题是$stmt->num_rows无效,除非您先存储结果,如下所述: mysqli_stmt::$num_rows - 返回语句结果集中的行数

fetch工作正常,并按预期返回数据行。唉,mun_rows属性为零,因此isUserRegistered方法失败。即使它返回了正确的数据。

注意:更改为实际检查fetch返回的值。

结论:

  • 使用$stmt->store_result()确保$stmt->num_rows有用。

  • 使用实际返回的数据来确保它返回了您期望的内容?

示例代码:http://pastebin.com/wDvAru39

我使用的最终isUserRegistered代码:

法典:

class UserRegister {
    protected $conn = null;
    public function __construct($dbConnection) 
    {
        $this->conn = $dbConnection;
    }   
    public function isUserRegistered($user_id) {
        print $user_id;
        $result_user_id = null;
        $stmt = $this->conn->prepare("SELECT user_id FROM users WHERE user_id = ?");
        $stmt->bind_param("s", $user_id);
        if ($stmt->execute()) {
            $stmt->store_result(); // need this to force the `num_rows` to be correct
            $stmt->bind_result($result_user_id);
            $stmt->fetch();     
            $no_of_rows = $stmt->num_rows; 
            var_dump(__METHOD__, 
                    'input user id: '. $user_id, 
                    'found user id: '. $result_user_id, 
                    'reported number of rows: '. ($no_of_rows),  __FILE__.__LINE__);
            $stmt->close();
            if (!empty($result_user_id)) { // check the returned data not the indicator
                print "Finally";
                // user already registered
                return true;
            } else {
                print "Stupid";
                // user is not registered
                return false;
            }
        }
    }    
}

var_dump语句的输出:

注意:store_result语句。

12321
string 'UserRegister::isUserRegistered' (length=30)
string 'input user id: 12321' (length=20)
string 'found user id: 12321' (length=20)
string 'reported number of rows: 1' (length=26)
string 'K:'developer'testmysql'index4.php78' (length=35)
Finally
string 'isUserRegistered : 

注释掉"store_result"语句给出:

12321
string 'UserRegister::isUserRegistered' (length=30)
string 'input user id: 12321' (length=20)
string 'found user id: 12321' (length=20)
string 'reported number of rows: 0' (length=26)
string 'K:'developer'testmysql'index4.php79' (length=35)
Finally
string 'isUserRegistered : true

注意:报告的行数为零。

大多数教程都是由知识比你好不了多少的人编写的。在代码中使用 num_rows() 是此类教程的明确标志。

事实上,在PHP中,你永远不需要这样的函数。

以你的代码为例。已经有一个变量可以判断用户是否已注册 - 选定的用户 ID。您可以从函数中返回。或者你可以只使用一个常量值,比如

public function isUserRegistered($user_id)
{
    $sql = "SELECT 1 FROM users WHERE user_id = ?";
    $stmt = $this->conn->prepare($sql);
    $stmt->bind_param("s", $user_id);
    $stmt->execute();
    $stmt->bind_result($found);
    $stmt->fetch();     
    return $found;
}