num_rows 仅适用于 `store_result()`
num_rows is working only with `store_result()`
num_rows
仅在我使用 mysqli->store_result()
时有效。这意味着它仅适用于缓冲结果。有人可以解释为什么会这样吗?
在下面的代码中,当我使用 mysqli->store_result()
然后 num_rows
工作。否则显示 0
.
function retrievearticle($mysqli, $articleid)
{
echo $articleid;
$query="select ArticleText,ArticleTitle from article where ArticleId=?";
$stmt=$mysqli->stmt_init();
$stmt->prepare($query);
$stmt->bind_param('i', $articleid);
$stmt->execute();
$stmt->store_result();
echo "here 2";
$stmt->bind_result($ArticleText, $ArticleTitle);
$stmt->fetch();
echo $stmt->num_rows;
}
As per this comment in the PHP
manual, here's an explanation of what you are facing :
Please be advised, for people who sometimes miss to read this important Manual entry for this function:
If you do not use mysqli_stmt_store_result( ), and immediatley call this function after executing a prepared statement, this function will usually return 0 as it has no way to know how many rows are in the result set as the result set is not saved in memory yet.
mysqli_stmt_store_result( ) saves the result set in memory thus you can immedietly use this function after you both execute the statement AND save the result set.
If you do not save the result set but still want to use this function you have to actually loop through the result set one row at a time using mysqli_stmt_fetch( ) before using this function to determine the number of rows.
A thought though, if you want to determine the number of rows without storing the result set and after looping through it, why not just simply keep an internal counter in your loop every time a row is fetched and save the function call.
In short, this function is only really useful if you save the result set and want to determine the number of rows before looping through it, otherwise you can pretty much recreate its use like I suggested.
num_rows
仅在我使用 mysqli->store_result()
时有效。这意味着它仅适用于缓冲结果。有人可以解释为什么会这样吗?
在下面的代码中,当我使用 mysqli->store_result()
然后 num_rows
工作。否则显示 0
.
function retrievearticle($mysqli, $articleid)
{
echo $articleid;
$query="select ArticleText,ArticleTitle from article where ArticleId=?";
$stmt=$mysqli->stmt_init();
$stmt->prepare($query);
$stmt->bind_param('i', $articleid);
$stmt->execute();
$stmt->store_result();
echo "here 2";
$stmt->bind_result($ArticleText, $ArticleTitle);
$stmt->fetch();
echo $stmt->num_rows;
}
As per this comment in the PHP
manual, here's an explanation of what you are facing :
Please be advised, for people who sometimes miss to read this important Manual entry for this function:
If you do not use mysqli_stmt_store_result( ), and immediatley call this function after executing a prepared statement, this function will usually return 0 as it has no way to know how many rows are in the result set as the result set is not saved in memory yet.
mysqli_stmt_store_result( ) saves the result set in memory thus you can immedietly use this function after you both execute the statement AND save the result set.
If you do not save the result set but still want to use this function you have to actually loop through the result set one row at a time using mysqli_stmt_fetch( ) before using this function to determine the number of rows.
A thought though, if you want to determine the number of rows without storing the result set and after looping through it, why not just simply keep an internal counter in your loop every time a row is fetched and save the function call.
In short, this function is only really useful if you save the result set and want to determine the number of rows before looping through it, otherwise you can pretty much recreate its use like I suggested.