获取从 CodeIgniter 1.7 中的 ActiveRecord 构建的原始 SQL 查询

问题描述 投票:0回答:5

我正在尝试在我的第一个严肃的 CodeIgniter 应用程序中调试一些代码,但我似乎找不到在哪里可以简单地获取我的 ActiveRecord 代码刚刚生成的原始 SQL。

$where  = 'DAY(`datetime_start`) = '. date('d',$day) .' AND ';
$where .= 'MONTH(`datetime_start`) = '. date('m',$day) .'';

$this->db->from('events')->where($where);
$result = $this->db->get();
php sql codeigniter activerecord
5个回答
45
投票

在查询运行之前:

$this->db->_compile_select(); 

运行后:

$this->db->last_query();

10
投票

当然,我在发布后 2 分钟就找到了它,感谢 Phil Sturgeon

echo $this->db->last_query();

8
投票

此外,您可以将以下内容放入控制器中:

$this->output->enable_profiler(TRUE);

您会收到询问以及更多信息。


1
投票

对于任何发现这篇旧文章并想知道最新 v3 中这是什么的人,函数是 $this->db->get_compiled_select()。


-2
投票

在 CI 的下一个版本发布之前你可以做这样的事情

private function get_query_string()
{
    return  'SELECT ' . 
            implode( ' , ' , $this->db->ar_select ) .
            ' FROM ' .
            implode( ' , '  , $this->db->ar_from ) .
            ( count( $this->db->ar_where ) ? ' WHERE ' : '' ) .
            implode( ' ' , $this->db->ar_where );
}
© www.soinside.com 2019 - 2024. All rights reserved.