Catch Database Error Codeigniter
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 codeigniter catch database exception about Stack Overflow the company Business Learn more about hiring developers or posting ads codeigniter catch db error with us Stack Overflow Questions Jobs Documentation Tags Users Badges Ask Question x Dismiss Join the Stack Overflow Community Stack Overflow
Show Database Error In Codeigniter
is a community of 4.7 million programmers, just like you, helping each other. Join them; it only takes a minute: Sign up Codeigniter - handling errors when using active record up vote 43 down
How To Get Database Error In Codeigniter
vote favorite 13 I am putting together a few models for my codeigniter site and can't seem to find any word in the documentation of how to handle errors that could occur when using the Active Record system. The documentation demonstrates how to perform CRUD along with some relatively involved queries but no where along the line is error handling discussed. I have done a quick google search and codeigniter active record error handling it would appear that the Active Record classes do not throw exceptions. Is this the case? No try catch then... So, how do you code to handle database errors in codeigniter? (failed connection, duplicate key, broken referential integrity, truncation, bad data types etc etc) php codeigniter activerecord share|improve this question edited Nov 22 '15 at 20:19 Josh Crozier 98.5k26144146 asked Feb 4 '10 at 23:58 Sergio 3,37484577 add a comment| 2 Answers 2 active oldest votes up vote 53 down vote accepted Whether you're using the active record class or not, you can access database errors using $this->db->_error_message() and $this->db->_error_number(). If you're using a mysql database, these functions are equivalent to mysql_error() and mysql_errno() respectively. You can check out these functions by looking at the source code for the database driver for the database you're using. They're located in system/database/drivers. So, after you run a query, you can check for errors using something like: if ($this->db->_error_message()) \\handle error share|improve this answer answered Feb 5 '10 at 6:53 emmychan 1,548108 18 FYI - This only works if your db_debug flag in the database config file is set to FALSE. By default its set to TRUE in a new installation of CI 2.1 –Felby Apr 5 '1
here for a quick overview of the site Help Center Detailed answers to any questions you might have Meta Discuss
Codeigniter Get Mysql Error
the workings and policies of this site About Us Learn more codeigniter try catch about Stack Overflow the company Business Learn more about hiring developers or posting ads with us Stack Overflow codeigniter insert error-handling Questions 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 http://stackoverflow.com/questions/2204140/codeigniter-handling-errors-when-using-active-record other. Join them; it only takes a minute: Sign up How to catch DB errors in CodeIgniter PHP up vote 5 down vote favorite 1 I am new to CodeIgniter, PHP and MySQL. I want to handle the DB generated errors. From one of the post in Stackoverflow, I knew that by following statement one can catch the http://stackoverflow.com/questions/17923382/how-to-catch-db-errors-in-codeigniter-php error. $this->db->_error_message(); But I cannot figure out the exact syntax of using that. Suppose I want to update the records of table named "table_name" by the following statement: $array['rank']="8"; $array['class']="XII"; $this->db->where('roll_no',$roll_no); $this->db->update("table_name", $array); Here in the above code I want to catch the DB error whenever any DB level violation occurs i.e. either field name is not valid or some unique constraint violation occurs. If anyone helps me to fix that I would be really grateful. Thank you. php mysql codeigniter share|improve this question asked Jul 29 '13 at 11:56 Joy 66241730 Have you tried try..catch block? Catch(Exception $e) { echo $e->getMessage(); } –timenomad Jul 29 '13 at 12:00 add a comment| 2 Answers 2 active oldest votes up vote 5 down vote You can debug the database error on database configuration in (config/database.php) like this: $db['default']['db_debug'] = TRUE; More info read here Also you can use Profiler to see all the queries and their speed. In controller you can put this: $this->output->enable_profiler(TRUE); More information read here share|improve this ans
full version with proper formatting. RastoStric02-23-2015, 04:03 AM I am aware of the discussions on this topic in the CI forum, StackOverflow and elsewhere. I tried to do my database error research carefully but it seems to me, that I did not find a proper answer. The question is how to handle database transaction errors and not to loose the error details. database error in 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)
{