php 在 CodeIgniter 1.7 中获取原始 SQL 查询

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1507710/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 02:52:09  来源:igfitidea点击:

Getting raw SQL Queries in CodeIgniter 1.7

phpcodeigniter

提问by bkorte

I'm trying to debug some code in my first serious CodeIgniter app and I can't seem to find where I can simply get the raw SQL that my ActiveRecord code just generated.

我正在尝试在我的第一个严肃的 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();

Thanks for the help!

谢谢您的帮助!

回答by hipertracker

Before the query runs:

在查询运行之前:

$this->db->_compile_select(); 

And after it has run:

在它运行之后:

$this->db->last_query();

回答by bkorte

Of course, I found it 2 minutes after posting, courtesy of Phil Sturgeon.

当然,我在发帖 2 分钟后找到了它,由Phil Sturgeon 提供

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

回答by John McCollum

Also, you can put the following in your controller:

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

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

You'll get queries and a lot more.

你会得到查询和更多。

回答by Ukuser32

For anyone finding this old post and wondering what this is in the latest v3 the function is $this->db->get_compiled_select().

对于任何找到这篇旧帖子并想知道最新 v3 中的内容的人来说,函数是 $this->db->get_compiled_select()。

回答by Babak Bandpay

You can make something like this until CI's next version is released

你可以做这样的事情,直到 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 );
}