我有一个函数来获取数据库中的记录,如下所示:
public function test_records_with_join($select, $from, $joins=array(), $where, $order_by = null, $limit=null, $offset=null, $or_where=null, $group_by = null, $protect = TRUE){
if (!$protect) $this->db->_protect_identifiers=false;
if (is_array($select)){
foreach ($select as $select){
$this->db->select($select, false);
}
}else{
$this->db->select($select, false);
}
$this->db->from($from);
if (is_array($joins) && count($joins)>0){
foreach ($joins as $join){
$this->db->join($join[0],$join[1],$join[2]);
}
}
foreach ($where as $key => $value) {
if ($key=='OR_WHERE') {
foreach ($value as $or_where_condition){
$this->db->where($or_where_condition);
}
}
else $this->db->where($key, $value, false);
}
if (!is_null($order_by)) $this->db->order_by($order_by);
if (!is_null($group_by)) $this->db->group_by($group_by);
if (!is_null($limit) && !is_null($offset)) $this->db->limit($limit, $offset);
$result = $this->db->get();
**Debug::dump($result);**
if (isset($data)) unset($data);
$data = array();
foreach ($result->result() as $row){
$data[] = $row;
}
echo count($data);
return $data;
}
查询1:
$query1 = $this->_product_model->test_records_with_join('GROUP_CONCAT(pr.id) as promo_ids, pr.updated_date, p.*, b.title as brand_title',
'products as p',
array(
array('product_brands as b','b.id=p.brand_id','left'),
array('promos as pr','FIND_IN_SET(p.id, pr.product_ids)','left')
),
$where, $order_by, null, null, null, 'p.id', FALSE);
$query2 = $this->_product_model->test_records_with_join('GROUP_CONCAT(pr.id) as promo_ids, pr.updated_date, p.*, b.title as brand_title',
'products as p',
array(
array('product_brands as b','b.id=p.brand_id','left'),
array('promos as pr','FIND_IN_SET(p.id, pr.product_ids)','left')
),
$where, $order_by, 40, 0, null, 'p.id', FALSE);
这是最后执行的查询:
SELECT GROUP_CONCAT(pr.id) as promo_ids, pr.updated_date, p.*, b.title as brand_title
FROM products as p
LEFT JOIN product_brands as b ON b.id=p.brand_id
LEFT JOIN promos as pr ON FIND_IN_SET(p.id, pr.product_ids)
WHERE p.visible = 1
AND p.price>=0
AND p.price<=2500000
GROUP BY p.id
ORDER BY p.price DESC
LIMIT 40
更新
执行这些查询给了我这个:
object(CI_DB_mysql_result)#150 (8) {
["conn_id"] => resource(40) of type (mysql link)
["result_id"] => resource(189) of type (mysql result)
["result_array"] => array(0) {
}
["result_object"] => array(0) {
}
["custom_result_object"] => array(0) {
}
["current_row"] => int(0)
["num_rows"] => int(720)
["row_data"] => NULL
}
int(720)
object(CI_DB_mysql_result)#1596 (8) {
["conn_id"] => resource(40) of type (mysql link)
["result_id"] => resource(191) of type (mysql result)
["result_array"] => array(0) {
}
["result_object"] => array(0) {
}
["custom_result_object"] => array(0) {
}
["current_row"] => int(0)
["num_rows"] => int(40)
["row_data"] => NULL
}
使用第二个查询($ per_page = 40,$ offset = 0),$ data中没有任何内容(计算它什么都不返回)。为什么?在重新初始化之前,我试图取消设置$ data,但没有发生任何事情。
我哪里做错了?提前谢谢!