$ result-> num_rows始终返回0 [英] $result->num_rows always returns 0
问题描述
在阅读了大量的主题,教程和其他内容之后-我想在这里发布,希望有人可以帮助我. 我尝试了所有可能得到的建议,但仍然没有用.
after reading tons of threads, tutorials and whatever - I feel like posting here and hope that someone can help me. I tried out every advice I could get, but it's still not working.
这是我的代码:
$prep_stmt = "SELECT id, DATE_FORMAT(added,'%d.%M'), title FROM offers ORDER BY added DESC LIMIT ?, ?;";
$stmt = $mysqli->prepare($prep_stmt);
$stmt->bind_param ('ii',$lowlimit, $page);
$stmt->execute();
$stmt->bind_result($id, $added, $title);
while ($stmt->fetch()) {
... # some random output here
}
$count = $stmt->num_rows;
echo "c -> ". $count;exit;
我总是得到"c-> 0" ...但是已经有输出了...所以我在做什么错呢? :/
I always get "c -> 0" ... but there IS output already ... so what am I doing wrong ? :/
推荐答案
在访问num_rows
属性之前,您需要调用store_result()
方法.
You need to call the store_result()
method before accessing the num_rows
property.
来自对 PHP手册文档:
如果不使用
mysqli_stmt_store_result()
,并在执行准备好的语句后立即调用此函数,则该函数通常将返回0,因为它无法知道结果集中有多少行,因为没有结果集保存在内存中了.
If you do not use
mysqli_stmt_store_result()
, and immediately 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()
将结果集保存在内存中,因此您可以在执行语句并保存结果集后立即使用此功能.
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.
您的代码应如下所示:
$stmt->execute();
$stmt->store_result();
$stmt->bind_result($id, $added, $title);
while ($stmt->fetch()) {
# some random output here...
}
$count = $stmt->num_rows;
这篇关于$ result-> num_rows始终返回0的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!