我正在尝试计算从数据库返回的行。当我运行此代码时,这将返回包含用户名和密码的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();
答案 0 :(得分:1)
在获取num_rows之前使用$stmt->store_result();
。
答案 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();