OOP Mysqli num_rows总是返回0

时间:2014-07-15 12:32:13

标签: php

我正在尝试计算从数据库返回的行。当我运行此代码时,这将返回包含用户名和密码的1行,但是当我尝试计算行时,即使数据库实际上返回行,它也总是返回零。

$row_count =$stmt -> num_rows仅返回0.

$stmt = $mysqli -> prepare
("SELECT username, password FROM members WHERE username=? AND password=?");
$stmt -> bind_param("ss", $username, $password);
$stmt -> execute();
$stmt -> bind_result($returned_username, $returned_password);   
$stmt->fetch();

$row_count = $stmt -> num_rows;
echo $row_count;

echo $returned_username;
echo "<br />";
echo $returned_password;

$stmt -> close();   
$mysqli ->close();

2 个答案:

答案 0 :(得分:1)

在获取num_rows之前使用$stmt->store_result();

更多信息:http://php.net/manual/en/mysqli-stmt.num-rows.php

答案 1 :(得分:0)

试试这个:

$stmt = $mysqli -> prepare
("SELECT username, password FROM members WHERE username=? AND password=?");
$stmt -> bind_param("ss", $username, $password);
$stmt -> execute();
$stmt -> store_result(); //You need to store the results first
$stmt -> bind_result($returned_username, $returned_password);   
$stmt->fetch();

$row_count = $stmt -> num_rows;
echo $row_count;

echo $returned_username;
echo "<br />";
echo $returned_password;

$stmt -> close();   
$mysqli ->close();