使用PHP / CodeIgniter在MySQL中获取最后执行的查询

时间:2022-02-03 00:52:39

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?

谁能告诉我怎样才能得到我的最后一个查询?

4 个解决方案

#1


40  

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

从查询辅助函数手册中获取的示例

#2


4  

Other useful functions if your are debugging are

如果您正在调试其他有用的功能

mysql_errno();  // Returns the error number of the last MySQL operation 3
mysql_error();  // Returns the error description of the last MySQL operation
mysql_info();  // Returns information about the last query

#3


1  

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的示例。

#4


1  

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()函数。

#1


40  

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

从查询辅助函数手册中获取的示例

#2


4  

Other useful functions if your are debugging are

如果您正在调试其他有用的功能

mysql_errno();  // Returns the error number of the last MySQL operation 3
mysql_error();  // Returns the error description of the last MySQL operation
mysql_info();  // Returns information about the last query

#3


1  

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的示例。

#4


1  

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()函数。