Php 即使数据存在,Prepared语句也不返回行

Php 即使数据存在,Prepared语句也不返回行,php,mysqli,Php,Mysqli,您好,我应该如何使这项工作: 我想知道为什么当我使用预先准备好的语句时它不起作用。我尝试了使用查询方法,结果很有效 以下是我尝试过的: $query = new mysqli('localhost','root', '', 'sample'); $rows = $query->query('SELECT * FROM `sample`.`sample_user` WHERE `userName` = "test" AND `userPass` = "data"'); echo $rows

您好,我应该如何使这项工作:

我想知道为什么当我使用预先准备好的语句时它不起作用。我尝试了使用查询方法,结果很有效

以下是我尝试过的:

$query = new mysqli('localhost','root', '', 'sample');

$rows = $query->query('SELECT * FROM `sample`.`sample_user` WHERE `userName` = "test" AND `userPass` = "data"');
echo $rows->num_rows; // this returns 1 since I have this record from my database
我试图通过使用一个事先准备好的语句来增强它:

$query = new mysqli('localhost','root', '', 'sample');
$prepared = $query->prepare('SELECT * FROM `sample`.`sample_user` WHERE `userName` = ? AND `userPass` = ?');

$prepared->bind_param("ss", $userName, $userPass );
$prepared->execute();
echo $prepared->num_rows; //this returns 0

我被这个问题缠住了。也许我遗漏了什么。

是的,您需要返回结果,绑定只是将查询绑定到您提供的参数

 /* Get the result */
 $result = $stmt->get_result();

 /* Get the number of rows */
 $num_of_rows = $result->num_rows;

查阅副本;有几件事你没有做/用。@FunkFortyNiner你能关闭这个线程吗。你是什么意思?我不明白你说的“你能合上这条线”是什么意思。我已经解决了这个问题。我可以结束这个问题吗?我已经用一个复本结束了这个问题,它向你展示了如何正确地做这件事。您没有存储或绑定结果。