Php Codeigniter活动记录查询不工作
我有以下模型方法:Php Codeigniter活动记录查询不工作,php,mysql,codeigniter,activerecord,codeigniter-2,Php,Mysql,Codeigniter,Activerecord,Codeigniter 2,我有以下模型方法: public function product_count_where($where,$countwhat){ $fields='count('.$countwhat.') as totalcount'; try{ $this->db->select($fields); $this->db->from('product as p');
public function product_count_where($where,$countwhat){
$fields='count('.$countwhat.') as totalcount';
try{
$this->db->select($fields);
$this->db->from('product as p');
$this->db->join('shop_product_m2m as m2m','p.id = m2m.product','left');
$this->db->join(
'price_source_product_m2m as psp','p.id = psp.product','left');
$this->db->where($where);
$this->db->group_by('p.id');
$query=$this->db->get();
if(!$query) {
$errno =mysqli_errno($this->db->conn_id);
$errmsg=mysqli_error($this->db->conn_id);
throw new Exception('Error'.$errno." says:".$errmsg, 1);
}else{return $query->row_array();}
}catch(Exception $e){return $e->getMessage();}
}
我从一个视图调用这个函数(当然有一个助手函数,我已经定义了从视图调用模型函数),如下所示:
<?php
$where='p.category = '.$item['category'];
$where=$where.' AND ( m2m.price BETWEEN '. round($item['lowerprice']) .
' AND '. round($item['upperprice']) .
' OR psp.price BETWEEN '. round($item['lowerprice']) .
' AND '. round($item['upperprice']) .' )';
$countprodbybudget=product_count_where($where,'p.id');
echo $countprodbybudget;
?>
现在,令人惊讶的是,我愚蠢而顽固的方法(或者是我)对任何值都返回1,即使我排除了betwwen子句部分,它仍然返回1
我已经浪费了两个小时调查这件事。。有人能帮我找出我在这里犯的错误吗?你应该先做一个
echo$this->db->last_query()代码>以查看您的请求。我们帮不了你,你这里有太多的代码和太多我们不知道的变量。@VincentDecaux我检查了很多次,但我没有发现任何错误。。请查看最后一个查询的编辑。如果您将此查询放入mysql(通过命令行或phpmyadmin/heidisql/etc),会发生什么情况?@George Sazanovich我已经从命令行运行了此操作,它带来了预期的结果。我想既然您得到了返回的对象,就需要回显$countprodbybudget->totalCount;
SELECT count(p.id) as totalcount FROM (`product` as p) LEFT JOIN
`shop_product_m2m` as m2m ON `p`.`id` = `m2m`.`product` LEFT JOIN
`price_source_product_m2m` as psp ON `p`.`id` = `psp`.`product`
WHERE `p`.`category` = 1 AND ( m2m.price BETWEEN 10000 AND 15000 OR
psp.price BETWEEN 10000 AND 15000 ) GROUP BY `p`.`id`