CodeIgniter Possible to Show MySQL Query in Error Log?
I always end up with errors in my CodeIgniter log such as
ERROR - 2011-04-12 00:06:44 --> Query error: Duplicate entry '1391280167' for key 1
开发者_如何学JAVA
But it's not much help without context. Is there any way to record the query as well that caused this error?
I just took a quick peek inside the system/database/DB_Driver.php
(line 323) file and it doesn't log the SQL query with any settings you choose. It should however print these messages to the screen if you are in db_debug mode.
If you don't mind messing with the files in the CI system folder, you could get the sql statement in your log file by changing system/database/DB_Driver.php
(line 323, for CI 2.0.1) to:
log_message('error', 'Query error: '.$error_msg. ' - '. $sql);
As a start, you can get the last query that was run using $this->db->last_query()
So we could get the query as a string with the following snippet:
// strip out line returns, new lines and tabs
$query = str_replace( array("\r", "\n", "\t"), '', trim($this->db->last_query()) );
Once you have a string, we can log to the error log:
error_log( "Last database query: " . $query );
Note: Depending on your application logic and traffic, this could add tons of extra bloat to the error log over time, so I would recommend this is only used as a temporary troubleshooting tool.
Hope that helps.
Check if you have defined Auto Increment for the Primary Key, in the table in which you insert.
精彩评论