php 如何在codeigniter模型中打印SQL语句
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6142099/
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
How to print SQL statement in codeigniter model
提问by Technupe
I have a sql statement in my model,
我的模型中有一个 sql 语句,
I then say
然后我说
$query = $this->db->query($sql, array(fields, fields1);
if ($query) {
return true:
} else {
echo "failed";
return false;
}
My query always fails, how do I get php to print the exact sql statement being sent to my database? And display that on my php view, page
我的查询总是失败,我如何让 php 打印发送到我的数据库的确切 sql 语句?并将其显示在我的 php 视图页面上
回答by chhameed
You can use this:
你可以使用这个:
$this->db->last_query();
"Returns the last query that was run (the query string, not the result)."
“返回上次运行的查询(查询字符串,而不是结果)。”
Reff: https://www.codeigniter.com/userguide3/database/helpers.html
参考资料:https://www.codeigniter.com/userguide3/database/helpers.html
回答by Novo
To display the query string:
显示查询字符串:
print_r($this->db->last_query());
To display the query result:
显示查询结果:
print_r($query);
The Profiler Class will display benchmark results, queries you have run, and $_POST data at the bottom of your pages. To enable the profiler place the following line anywhere within your Controller methods:
Profiler 类将在页面底部显示基准测试结果、您运行的查询和 $_POST 数据。要启用探查器,请将以下行放置在您的 Controller 方法中的任何位置:
$this->output->enable_profiler(TRUE);
Profiling user guide: https://www.codeigniter.com/user_guide/general/profiling.html
分析用户指南:https: //www.codeigniter.com/user_guide/general/profiling.html
回答by pedro
You can display the ActiveRecord generated SQL:
可以显示 ActiveRecord 生成的 SQL:
Before the query runs:
在查询运行之前:
$this->db->_compile_select();
And after it has run:
在它运行之后:
$this->db->last_query();
回答by Programmer
if you need a quick test on your query, this works great for me
如果您需要对查询进行快速测试,这对我很有用
echo $this->db->last_query(); die;
回答by Christian Dechery
After trying without success to use _compiled_select()
or get_compiled_select()
I just printed the db
object, and you can see the query there in the queries
property.
在尝试使用但没有成功之后,_compiled_select()
或者get_compiled_select()
我只是打印了db
对象,您可以在queries
属性中看到查询。
Try it yourself:
自己试试:
var_dump( $this->db );
If you know you have only one query, you can print it directly:
如果你知道你只有一个查询,你可以直接打印它:
echo $this->db->queries[0];
回答by Muhammad Sadiq
You can simply use this at the end..
你可以简单地在最后使用它..
echo $this->db->last_query();
回答by Naveed
There is a new public method get_compiled_select
that can print the query before running it. _compile_select
is now protected therefore can not be used.
有一个新的公共方法get_compiled_select
可以在运行之前打印查询。_compile_select
现在受保护因此不能使用。
echo $this->db->get_compiled_select(); // before $this->db->get();
回答by Laurence Cope
Neither last_query()
or get_compiled_select()
works for me, so a slight change of pedro's code works for me just fine. Do not include ->get()
in your build, this must be before the ->get()
无论是last_query()
或get_compiled_select()
为我工作,所以佩德罗的代码的微小变化对我的作品就好了。不要包含->get()
在你的构建中,这必须在 ->get() 之前
echo $this->EE->db->_compile_select();
回答by Chanble
I try to @Chumillas's answer and @chhameed's answer, but it not work,because the sql is wrong.So I found new approach,like this:
我尝试@Chumillas's answer和@chhameed's answer,但它不起作用,因为sql是错误的。所以我找到了新方法,如下所示:
- Insert
echo $sql; flush(); exit;
into beforereturn $sql;
_compile_select
function ofDB_active_rec.php
- 插入
echo $sql; flush(); exit;
到return $sql;
_compile_select
函数之前DB_active_rec.php
回答by larp
Add this line right after the query you want to print.
在要打印的查询之后立即添加此行。
Example:
例子:
$query = $this->db->query('SELECT * FROM table WHERE condition');
$query = $this->db->query('SELECT * FROM table WHERE condition');
//Add this line.
//添加这一行。
var_dump($this->db->last_query());
var_dump($this->db->last_query());
exit();
出口();
or
或者
echo $this->db->last_query();
回声 $this->db->last_query();