我想知道如何使用LIMIT
將SELECT FOUND_ROWS()
包含到現有查詢中,或者如果有更好的方法可以獲得總行數而不需要LIMIT
。mysqli - 使用SELECT FOUND_ROWS()和預處理語句
//SELECT * just for question
$q = $this->db->mysqli->prepare("SELECT SQL_CALC_FOUND_ROWS *
FROM countries_ship c
JOIN items i
ON c.item_id = i.id
JOIN item_expire e
ON c.item_id = e.item_id
JOIN users u
ON i.user_id = u.id
LEFT JOIN bids b
ON i.id = b.item_id
LEFT JOIN publishers p
ON i.item_publisher = p.id
LEFT JOIN tags_rel tr
ON c.item_id = tr.item_id
JOIN tags t
ON t.id = tr.tag_id
LEFT JOIN countries co
ON i.item_location = co.id
WHERE ".$where."
GROUP BY i.id ORDER BY ".$order." ".$limit."");
$count_rows = $this->db->mysqli->query("SELECT FOUND_ROWS()");
/* return of var_dump($count_rows);
object(mysqli_result)#74 (5) {
["current_field"]=>
int(0)
["field_count"]=>
int(1)
["lengths"]=>
NULL
["num_rows"]=>
int(1)
["type"]=>
int(0)
}
*/
$prep = join("", $prep);
call_user_func_array('mysqli_stmt_bind_param', array_merge (array($q, $prep), $this->helperClass->valRef($ref)));
$q->execute();
$rows = $this->helperClass->bindResults($q);
$q->close();
return $rows;
不知道這是否100%有效,但這是我如何解決我的問題。
- 改變了查詢開始
SELECT SQL_CALC_FOUND_ROWS
- 創建新的陣列 -
$resArray = array()
移動的第二查詢類似建議,並添加這兩種結果
$resArray
。$q->execute(); $rows = $this->helperClass->bindResults($q); $q->close(); $count_rows = $this->db->mysqli->query("SELECT FOUND_ROWS()"); $count = mysqli_fetch_assoc($count_rows); $resArray[] = $count; $resArray[] = $rows; return $resArray;
select count(*)as total_item from ... –
不會只是返回帶有限制的計數嗎? – Ciprian
就像您通常那樣:將'SQL_CALC_FOUND_ROWS'添加到您的'SELECT'併發出第二個查詢來獲得結果。 – jeroen