ci 中where条件
先上代码$this->db->select();
$this->db->from('test');
$where = "`is_del` =1 ";
$this->db->where($where);
$db = clone ($this->db);
$this->db->limit($limit, $offset);
$this->db->order_by('add_time', 'desc');
$ret['list'] = $this->db->get()->result_array();
$ret['total'] = $db->count_all_results();
//debug($ret['total']);
if ($ret) {
return $ret;
}
return FALSE;
这里$where 里一旦有语法错误,就报如下错误
Fatal error:Call to a member function result_array() on a non-object in
原来都是报 具体sql语句里哪个字段出错的提示,现在怎么总报这个错误呢。
一般建議, 輸出前先檢查資料筆數是否大於 0
這樣才能證明有獲得資料
有獲得資料才有辦法陣列輸出
//搜尋單筆資料
public function get_OneData($table,$th,$td){
$query = $this->db->where($th,$td)->get($table);
if($query->num_rows() == 1){
return $query->row_array();
}
return FALSE;
}
这个只是防止报$query->row_array();这类的错误,其实我是想要报错结果的,但是要对的上,不要因为where条件里字段名写错了就报这样的错误,这样都很难找到出错的地方。 灰兔子 发表于 2015-3-13 12:24
这个只是防止报$query->row_array();这类的错误,其实我是想要报错结果的,但是要对的上,不要因为where条 ...
參考 http://stackoverflow.com/ - Codeigniter - handling errors when using active record 这个提示是因为你在数据库配置(/application/config/database.php)设置了$db['default']['db_debug'] = FALSE;这样你的SQL语法有误时它不中断和输出MYSQL错误,而是直接往下执行因为查询报错了之后,$this->db->get所返回的结果就不是一个结果集了,所以会导致这个错误!建议解决方案:开发时打开SQL报错机制方便调试,正式上线时再关闭 gogogo1027 发表于 2015-3-13 17:27
这个提示是因为你在数据库配置(/application/config/database.php)设置了$db['default']['db_debug'] = FAL ...
我是为了用ci的手动事务才把debug设置为fasle的,忘了改过了。
页:
[1]