Codeigniter Log Database Error
Contents |
here for a quick overview of the site Help Center Detailed answers to any questions you might have Meta Discuss the workings and policies of this site About Us Learn more about codeigniter database error handling Stack Overflow the company Business Learn more about hiring developers or posting ads with
Codeigniter Catch Database Error
us Stack Overflow Questions Jobs Documentation Tags Users Badges Ask Question x Dismiss Join the Stack Overflow Community Stack Overflow is a database error occurred codeigniter a community of 4.7 million programmers, just like you, helping each other. Join them; it only takes a minute: Sign up Codeigniter catch database errors up vote 3 down vote favorite 1 I'm looking for show database error in codeigniter a way to catch all the database errors (if and when they occur) and sent an error report my email. For regular php error I extended the CI_Exceptions class and added my email sending code in addition to logging. But the database errors don't go trough CI_Exceptions but instead are logged directly from CI_DB_Driver->query() and I don't want to modify any file in the System folder. Also I don't want
Codeigniter Log Database Queries
to write logging code around every query the app does. codeigniter codeigniter-2 share|improve this question asked Jan 9 '13 at 13:06 Dan F. 9891119 add a comment| 1 Answer 1 active oldest votes up vote 5 down vote accepted I'd be tempted to extend CI_Exceptions show_error method to catch any errors passed with the 'error_db' template rather than hunting through the db driver. As you've already extended CI_Exceptions with email code this would seem like the best practice. function show_error($heading, $message, $template = 'error_general', $status_code = 500) { set_status_header($status_code); $message = '
'.implode('
', ( ! is_array($message)) ? array($message) : $message).'
'; if ($template == 'error_db') { // do email send here with $message } if (ob_get_level() > $this->ob_level + 1) { ob_end_flush(); } ob_start(); include(APPPATH.'errors/'.$template.'.php'); $buffer = ob_get_contents(); ob_end_clean(); return $buffer; } share|improve this answer answered Jan 10 '13 at 0:25 Timmytjc 237210 Thank you that works wonderfully. I just made a small edit. –Dan F. Jan 10 '13 at 10:02 add a comment| Your Answer draft saved draft discarded Sign up or log in Sign up using Google Sign up using Facebook Sign up using Email and Password Post as a guest Name Email Post as a guest Name Email discard By posting your answer,here for a quick overview of the site Help Center Detailed answers to any questions you might have Meta Discuss the codeigniter error handling workings and policies of this site About Us Learn more about codeigniter query error handling Stack Overflow the company Business Learn more about hiring developers or posting ads with us Stack Overflow Questions
Codeigniter Try Catch
Jobs Documentation Tags Users Badges Ask Question x Dismiss Join the Stack Overflow Community Stack Overflow is a community of 4.7 million programmers, just like you, helping each other. http://stackoverflow.com/questions/14235886/codeigniter-catch-database-errors Join them; it only takes a minute: Sign up Codeigniter: How to handle database error? up vote 0 down vote favorite I'm creating a web application using codeigniter and postgresql. I have this inside my database: user id name unique(name) When someone try to register with the same name, i get an error. How can i handle them, without http://stackoverflow.com/questions/26297374/codeigniter-how-to-handle-database-error displaying the codeigniter's error and showing instead my custom error? If i set $db['default']['db_debug'] = FALSE; i don't get any error of course, but is there a way to handle the db error or should i check myself if the table already contains an entry with that same name? codeigniter debugging share|improve this question asked Oct 10 '14 at 9:34 Federico Ponzi 86411334 migrated from webmasters.stackexchange.com Oct 10 '14 at 10:31 This question came from our site for pro webmasters. add a comment| 3 Answers 3 active oldest votes up vote 0 down vote accepted I dont know anything by codeigniter, but im going to assume the principle works the same: You first make a query like SELECT id FROM tablename WHERE name='SomeName' LIMIT 1, then you check the number of rows. This kind of checking is fairly normal. Control as much as you (sensebly) can to avoid errors down the road. Zero rows? Safe to insert. Not zero rows? Display something like 'username allready taken'. Example with some code: $check = mysqli_query("SELECT i
full version with proper formatting. RastoStric02-23-2015, 04:03 AM I am aware of http://forum.codeigniter.com/archive/index.php?thread-1255.html the discussions on this topic in the CI forum, StackOverflow and elsewhere. I tried to do my research carefully but it seems http://jigarjain.com/blog/saving-all-db-queries-in-codeigniter/ to me, that I did not find a proper answer. The question is how to handle database transaction errors and not to database error loose the error details. The CI guide suggested way is PHP Code: $this->db->trans_start();
$this->db->query('ANSQLQUERY...');
$this->db->query('ANOTHERQUERY...');
$this->db->trans_complete();
if($this->db->trans_status()===FALSE)
{
//generateanerror...orusethelog_message()functiontologyourerror
}
However if 'db_debug' in config/database.php is TRUE, the execution fails on the first failing query and the error handling code is never run. OK, so let's set 'db_debug' to FALSE. In this case the error handling code runs, but how do I find out more about the DB error (error code and message)? Again, the recommended way is to use $this->db->_error_message() and $this->db->_error_number(), but they return nothing when 'db_debug' is FALSE. And this is my pr
are couple of other ways to achieve this including MYSQL Query Logging, i preferred to choose query logging at PHP level with no specific purpose. In the below post, i will show how to retrieve all the queries executed by Codeigniter's Database Class along with their execution time and dump them into a text file for future reference. Since we would be making use of Hooks, i would recommend to read about it once incase if you are unaware of what they are. First we would be enabling the Hooks by changing the config file in codeigniter's application/config/ folder. To do so set the following value in config file PHP // Set the following value in applications/config/config.php file $config['enable_hooks'] = TRUE; 123456 // Set the following value in applications/config/config.php file $config['enable_hooks'] = TRUE; Then Open up your hooks.php file in config folder and add the following code in it PHP $hook['post_controller'] = array( // 'post_controller' indicated execution of hooks after controller is finished 'class' => 'Db_log', // Name of Class 'function' => 'logQueries', // Name of function to be executed in from Class 'filename' => 'db_log.php', // Name of the Hook file 'filepath' => 'hooks' // Name of folder where Hook file is stored ); 12345678910 $hook['post_controller'] = array( // 'post_controller' indicated execution of hooks after controller is finished'class' => 'Db_log', // Name of Class'function' => 'logQueries', // Name of function to be executed in from Class'filename' => 'db_log.php',// Name of the Hook file'filepath' => 'hooks' // Name of folder where Hook file is stored); Now we will create the hook with name db_log.php which we have set above and save it in application/hooks/ folder. Then we simply put the following code in this file // Name of Class as mentioned in $hook['post_controller] class Db_log { function __construct() { // Anything except exit() :P } // Name of function same as mentioned in Hooks Config function logQueries() { $CI = & get_instance(); $filepath = APPPATH . 'logs/Query-log-' . date('Y-m-d') . '.php'; // Creating Query Log file with today's date in application/logs folder $handle = fopen($filepath, "a+"); // Opening file with pointer at the end of the file $t