問題描述
我想從數(shù)據(jù)庫中獲取行數(shù),但是當(dāng)我嘗試這樣做時(shí),$g_check
變量將等于 0
并且我的代碼將回顯$sugg_title
消息位于 else
語句中.但是在數(shù)據(jù)庫中有 4 個(gè)插入的組,所以 num_rows
屬性應(yīng)該返回 4.
I want to get the number of rows from the database, but when I try to do this the $g_check
variable will be equal to 0
and my code will echo the $sugg_title
message which is in the else
statement. But in the database there are 4 inserted groups so the num_rows
property should return 4.
$sql = "SELECT DISTINCT gp.logo, gp.name
FROM gmembers AS gm
LEFT JOIN groups AS gp ON gp.name = gm.gname
WHERE gp.creator != ? AND gm.mname != ? LIMIT 10";
$stmt = $conn->prepare($sql);
$stmt->bind_param('ss',$log_username,$log_username);
$stmt->execute();
$g_check = $stmt->num_rows;
if ($g_check > 0){
$result = $stmt->get_result();
while ($row = $result->fetch_assoc()) {
$agList .= '<a href="group.php?g='.$row["name"].'"><img class="group_margin" src="groups/'.$row["name"].'/'.$row["logo"].'" alt="'.$row["name"].'" title="'.$row["name"].'" width="70" height="70" /></a>';
}
}else{
$sugg_title = "You have no group suggestions at the moment. Click ";
$sugg_title .= '<a href="all_groups.php">here</a> to view all groups.';
}
我將 strore_result()
和 fetch()
函數(shù)放在 execute()
之后,但隨后我收到此錯(cuò)誤消息:致命錯(cuò)誤:未捕獲的錯(cuò)誤:在布爾值上調(diào)用成員函數(shù) fetch_assoc()"
I put the strore_result()
and the fetch()
functions after execute()
but then I get this error message: "Fatal error: Uncaught Error: Call to a member function fetch_assoc() on boolean"
推薦答案
如果要使用mysqli_stmt::$num_rows
(即檢查prepared statement上的行數(shù)),你需要在執(zhí)行準(zhǔn)備好的語句后使用 $stmt->store_result()
才能檢查行數(shù).這意味著在我們檢查返回了多少行之前將結(jié)果存儲(chǔ)到內(nèi)存中.
If you want to use mysqli_stmt::$num_rows
(that is, check the number of rows on the prepared statement), you need to use $stmt->store_result()
after executing the prepared statement before being able to check the number of rows. That means that the result is stored into memory before we check how many rows was returned.
$stmt = $conn->prepare($sql);
$stmt->bind_param('ss',$log_username,$log_username);
$stmt->execute();
$stmt->store_result(); // Need to store the result into memory first
if ($stmt->num_rows) {
// ...
然而,如果你想使用mysqli_result::$num_rows
(在你從語句結(jié)果轉(zhuǎn)換的MySQLi-result上),你需要在執(zhí)行$result = $stmt->get_result();
,并使用$result->num_rows;
,如下所示.
However, if you want to use mysqli_result::$num_rows
(on the MySQLi-result you convert from the statement result), you need to do that after doing $result = $stmt->get_result();
, and use $result->num_rows;
, like shown below.
$stmt = $conn->prepare($sql);
$stmt->bind_param('ss',$log_username,$log_username);
$stmt->execute();
$result = $stmt->get_result();
if ($result->num_rows) {
while ($row = $result->fetch_assoc()) {
// ....
最后,他們都應(yīng)該做同樣的事情 - 提供原始準(zhǔn)備好的查詢返回的行數(shù).
In the end, they should both end up doing the same thing - provide a number of the rows returned by the original prepared query.
注意
請(qǐng)務(wù)必注意,您不能在同一語句中使用 store_result()
和 get_result()
.這意味著在第一個(gè)示例中,您不能轉(zhuǎn)換為 mysqli-result 對(duì)象(通過使用 get_result()
,它允許您使用標(biāo)準(zhǔn)的 fetch_assoc()
方法).由于 store_result()
將結(jié)果存儲(chǔ)到內(nèi)存中,get_result()
無需轉(zhuǎn)換,反之亦然.
Note
It's important to note that you cannot use store_result()
and get_result()
on the same statement. Which means that in the first example, you can not convert to a mysqli-result object (by using get_result()
, which allows you to use the standard fetch_assoc()
method). As store_result()
stores the result into memory, there are nothing for get_result()
to convert, and vice-versa.
這意味著如果你使用store_result()
,你需要通過statement-fetch,mysqli_stmt::fetch()
來獲取,并通過綁定結(jié)果>mysqli_stmt::bind_result()
.如果您使用 get_result()
,您應(yīng)該檢查結(jié)果 MySQLi-result 對(duì)象上的行數(shù)(如第二個(gè)示例所示).
This means that if you use store_result()
, you need to fetch through the statement-fetch, mysqli_stmt::fetch()
and bind the results though mysqli_stmt::bind_result()
. If you use get_result()
, you should check the number of rows on the resulting MySQLi-result object (as shown in the second example).
因此,您應(yīng)該構(gòu)建您的代碼,以便您只需要使用其中之一.
You should therefor construct your code such that you only need to use one of them.
話雖如此,使用 affected_rows
就像評(píng)論中建議的那樣,并不是適合這項(xiàng)工作的工具 - 根據(jù) mysqli_stmt::$affected_rows
上的手冊(同樣的事情適用于常規(guī)查詢,mysqli::$affected_rows
):
That being said, using affected_rows
like suggested in the comments, isn't the right tool for the job - as per the manual on mysqli_stmt::$affected_rows
(same thing applies for a regular query, mysqli::$affected_rows
):
返回受 INSERT、UPDATE 或 DELETE 查詢影響的行數(shù).
此函數(shù)僅適用于更新表的查詢.為了從 SELECT 查詢中獲取行數(shù),請(qǐng)改用 mysqli_stmt_num_rows()
.
Returns the number of rows affected by INSERT, UPDATE, or DELETE query.
This function only works with queries which update a table. In order to get the number of rows from a SELECT query, usemysqli_stmt_num_rows()
instead.
- PHP.net
mysqli_stmt::store_result()
- PHP.net
mysqli_stmt::get_result()
- PHP.net
mysqli_stmt::$num_rows
這篇關(guān)于使用 MySQLi 準(zhǔn)備語句時(shí)無法獲取行數(shù)和獲取的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,也希望大家多多支持html5模板網(wǎng)!