【发布时间】: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