0

我正在尝试从文字query调用过渡到准备好的语句。

我在课堂上有以下功能:

public function read ($identifier) {
    $stmt = static::$mysqli->prepare('SELECT * FROM `table` WHERE `id` = ?;');
    $stmt->bind_param('i', $identifier);
    $stmt->execute();

    var_dump($stmt->num_rows);
}

我从 phpMyAdmin 知道数据库中有 id 为 1 的行,但如果我调用read(1);num_rows则为 0。

但是,如果我使用这个逻辑:

public function read ($identifier) {
    $result = static::$mysqli->query('SELECT * FROM `table` WHERE `id` = '.(int) $identifier.';');
    var_dump($result->num_rows);
}

相反,我得到 1。

4

1 回答 1

2

$stmt->store_result();通话后尝试execute()通话。
执行后结果尚未存储在内存中,您必须获取所有行或调用变量store_result()num_rows显示实际受影响的行数。

于 2013-12-13T21:28:27.350 回答