問題描述
幾天前我做了一個代碼,包括 get_result()
來從我的數據庫中接收結果.今天我想添加它并修復一些錯誤.所以我嘗試使用 num_rows
來查看是否有任何返回.但為此我不得不使用 store_result()
.當我這樣做時,get_result()
只返回一個布爾值 false.當我注釋掉 store_result()
時,一切正常.我知道 >=
會搞砸.但我把 =
放在那里進行調試(注釋掉 store_result()
并查看發生了什么).所以這不是問題
I made a code a few days ago including get_result()
to receave the results from my database. Today I wantet to add to it and fix some errors. So I tried to use num_rows
to see if anything were returned. But for this I had to use store_result()
. And when I do this get_result()
just returns a boolean of false. When I comment out store_result()
everything works as it should.
I know that the >=
will mess it up. But I put the =
there for debugging(to comment out the store_result()
and see what happend). So that is not the problem
$sql = $this->connect();
$a = $sql->prepare("SELECT `name`, `title`, `comment`, `date` FROM `comment` WHERE `post`=?");
$a->bind_param("s", $id);
$a->execute();
$a->store_result();
if ($a->num_rows >= 0) {
$res = $a->get_result();
var_dump($res);
while ($row = $res->fetch_assoc()) {
$results[] = $row;
}
return $results;
} else {
return false;
}
推薦答案
使用 get_result()
代替 store_result()
,然后使用結果對象的 num_rows
:
Use get_result()
instead of store_result()
, and then use the result object's num_rows
:
$a->execute();
$res = $a->get_result();
if ($res->num_rows > 0) {
while ($row = $res->fetch_assoc()) {
$results[] = $row;
}
return $results;
} else {
return false;
}
這篇關于mysql 中的 store_result() 和 get_result() 返回 false的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!