PHP $stmt->num_rows 不适用于准备好的语句

1 php mysql oop prepared-statement

我想检查一下if ($numRows >= 1)它应该返回一些东西。

当我使用时$con->mysql("{QUERY}"),它有效。

但当我使用时$stmt = $con->prepare("{QUERY}"),它不起作用。

有人有线索吗?

工作方法

<?php
if ($result = $con->query("SELECT username FROM users WHERE username = 'test'")) {
    $numRows = $result->num_rows;

    echo $numRows;
}
?>
Run Code Online (Sandbox Code Playgroud)

结果:1

不起作用的方法

<?php
$name = 'test';

$stmt = $con->prepare("SELECT username FROM users WHERE username = ?");
$stmt->bind_param('s', $name);

$name = 'test';

$stmt->execute();

$numRows = $stmt->num_rows;

echo $numRows;
?>
Run Code Online (Sandbox Code Playgroud)

结果:0

Raj*_*aul 5

SELECT在调用该方法之前,您需要传输查询的结果集->num_rows()

// your code    

$stmt->execute();
$stmt->store_result();  
$numRows = $stmt->num_rows;

echo $numRows;
Run Code Online (Sandbox Code Playgroud)

这是参考: