0

我想在 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'
4

2 回答 2

2

The $this->db->last_query() line returns the last query that was run.

But your SQL log file show us that you have a error in your SQL query. So, your query was not run. The last_query() line can not return anything.

Query error: Unknown column 'template' in 'where clause'

Do you have a template column in your table?

于 2013-03-02T20:37:34.723 回答
0

- 简单的解决方案。- 如果您使用的是 firfox。比安装 firbug 并启用它,在 firebug 控制台中,您可以查看与您的请求相关的任何内容,例如 - 您的 http 请求 - 发布/获取数据 - 响应等

于 2015-08-01T10:15:01.193 回答