問題描述
我想從數據庫中獲取行數,但是當我嘗試這樣做時,$g_check
變量將等于 0
并且我的代碼將回顯$sugg_title
消息位于 else
語句中.但是在數據庫中有 4 個插入的組,所以 num_rows
屬性應該返回 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()
函數放在 execute()
之后,但隨后我收到此錯誤消息:致命錯誤:未捕獲的錯誤:在布爾值上調用成員函數 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上的行數),你需要在執行準備好的語句后使用 $stmt->store_result()
才能檢查行數.這意味著在我們檢查返回了多少行之前將結果存儲到內存中.
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
(在你從語句結果轉換的MySQLi-result上),你需要在執行$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()) {
// ....
最后,他們都應該做同樣的事情 - 提供原始準備好的查詢返回的行數.
In the end, they should both end up doing the same thing - provide a number of the rows returned by the original prepared query.
注意
請務必注意,您不能在同一語句中使用 store_result()
和 get_result()
.這意味著在第一個示例中,您不能轉換為 mysqli-result 對象(通過使用 get_result()
,它允許您使用標準的 fetch_assoc()
方法).由于 store_result()
將結果存儲到內存中,get_result()
無需轉換,反之亦然.
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()
來獲取,并通過綁定結果>mysqli_stmt::bind_result()
.如果您使用 get_result()
,您應該檢查結果 MySQLi-result 對象上的行數(如第二個示例所示).
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).
因此,您應該構建您的代碼,以便您只需要使用其中之一.
You should therefor construct your code such that you only need to use one of them.
話雖如此,使用 affected_rows
就像評論中建議的那樣,并不是適合這項工作的工具 - 根據 mysqli_stmt::$affected_rows
上的手冊(同樣的事情適用于常規查詢,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 查詢影響的行數.
此函數僅適用于更新表的查詢.為了從 SELECT 查詢中獲取行數,請改用 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
這篇關于使用 MySQLi 準備語句時無法獲取行數和獲取的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!