MySQLi准备语句查询问题

时间:2010-05-19 00:36:11

标签: php mysqli

我对MySQLi准备好的语句比较新,并且遇到了错误。拿这个代码:

$user = 'admin';
$pass = 'admin';

if ($stmt = $mysqli->query("SELECT * FROM members WHERE username='$user' AND     password='$pass'"))
{
echo $stmt->num_rows;
}

这将显示“1”,因为它应该。

下一段代码虽然返回“0”:

$user = 'admin';
$pass = 'admin';

if ($stmt = $mysqli->prepare("SELECT * FROM members WHERE username=? AND password=?"))
{
$stmt->bind_param("ss", $user, $pass);
$stmt->execute();
echo $stmt->num_rows;
}

任何想法为什么?

1 个答案:

答案 0 :(得分:1)

您需要在获得行数之前调用store_result()


$user = 'admin';
$pass = 'admin';

if ($stmt = $mysqli->prepare("SELECT * FROM members WHERE username=? AND password=?")) { $stmt->bind_param("ss", $user, $pass); $stmt->execute(); $stmt->store_result(); // add this line echo $stmt->num_rows; }