【问题标题】:PHP $stmt->num_rows doesnt work by prepared statements [duplicate]PHP $stmt->num_rows 不适用于准备好的语句[重复]
【发布时间】:2017-03-13 13:03:24
【问题描述】:

我想检查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;
}
?>

结果: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;
?>

结果:0

【问题讨论】:

  • WHERE username = 'tst' 并且您正在分配 $name = 'test'; 或者这只是一个错字?
  • @Fred-ii- 必须编辑一些东西,确实编辑过。你现在可以检查一下吗?
  • 您需要存储和绑定结果。查看它关闭的重复问题中的答案(它包含一个准备好的语句方法)和 Rajdeep 的答案。

标签: php mysql oop prepared-statement


【解决方案1】:

您需要在调用-&gt;num_rows() 方法之前传输SELECT 查询的结果集。

// your code    

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

echo $numRows;

这是参考:

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-05-18
    • 1970-01-01
    • 2020-06-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多