我想在 CodeIgniter 中打印出一条失败的 SQL 语句。在我的第一次尝试中,我使用了一个打印 db->last_query() 的 try/catch 块。但是,正如您从日志中看到的那样,没有打印 SQL 语句。我究竟做错了什么?
public function get($limit = NULL, $offset = NULL, $sort = NULL, $search = NULL)
{
try {
if ($limit !== NULL) $limit = (int) $limit;
if ($offset !== NULL) $offset = (int) $offset;
if (is_array($sort)) {
foreach ($sort as $field => $order) {
$this->db->order_by($field, $order);
}
}
if (is_array($search)) {
foreach ($search as $field => $match) {
$this->db->where($field, $match);
}
}
$this->db->select($this->select_fields);
$query = $this->db->get($this->table_name, $limit, $offset);
// Set the results
$this->last_query = $this->db->last_query();
$this->num_rows = $query->num_rows();
$this->result_array = $query->result_array();
$this->db_result = $query;
$this->error_number = $this->db->_error_number();
$this->error_message = $this->db->_error_message();
} catch(Exception $e) {
log_message('error',$e->getMessage());
log_message('error',$this->db->last_query());
$this->error_number = 500;
}
}
我得到的错误是:
DEBUG - 2013-03-02 15:16:50 --> DB Transaction Failure
ERROR - 2013-03-02 15:16:50 --> Query error: Unknown column 'template' in 'where clause'