我想知道如何使用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