使用 PHP/CodeIgniter 在 MySQL 中获取上次执行的查询
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6536431/
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
Get last executed query in MySQL with PHP/CodeIgniter
提问by soredive
How can I get last query I ran in MySQL in both Windows and Linux?
如何在 Windows 和 Linux 中获取我在 MySQL 中运行的最后一个查询?
I am working with PHP and CodeIgniter. In my_model.php
, I have:
我正在使用 PHP 和 CodeIgniter。在my_model.php
,我有:
$query1 = ( ...something... );
$query2 = ( ...something... );
$variables = ( .... something .... );
$this->db->query(" $query1 ... $variables .. $query2", array( $variables, ... ));
I need the last query executed right after the code snippet above.
我需要在上面的代码片段之后立即执行最后一个查询。
Can anyone tell me how can I get my last query?
谁能告诉我如何获得我的最后一个查询?
回答by Damien Pirsy
Use:
用:
$this->db->last_query();
Returns the last query that was run (the query string, not the result). Example:
$str = $this->db->last_query();
// Produces: SELECT * FROM sometable....
example taken from the manual on query helper functions
示例取自查询辅助函数手册
回答by progman
In the config/database.php the config array must be set: 'save_queries' => TRUE, if it is false not working the last_query() function.
在 config/database.php 中,必须设置 config 数组:'save_queries' => TRUE,如果它是 false,则 last_query() 函数不起作用。
回答by tplaner
In CodeIgniter there is a helper function $this->db->last_query();
.
在 CodeIgniter 中有一个辅助函数$this->db->last_query();
。
This will return the string of the last query.
这将返回最后一个查询的字符串。
But I think you might be asking how to obtain the result, in which case you'd do:
但我认为您可能会问如何获得结果,在这种情况下您会这样做:
$query1 = ( ...something... );
$query2 = ( ...something... );
$variables = ( .... something .... );
$query = $this->db->query(" $query1 ... $variables .. $query2", array( $variables, ... ));
foreach ($query->result() as $row) {
//code goes here
}
For more information, take a look a CI's examples.
有关更多信息,请查看CI 的示例。