Codeigniter Catch 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 try catch database error Stack Overflow the company Business Learn more about hiring developers or posting ads with codeigniter db error catch us Stack Overflow Questions Jobs Documentation Tags Users Badges Ask Question x Dismiss Join the Stack Overflow Community Stack Overflow is codeigniter database error handling a community of 4.7 million programmers, just like you, helping each other. Join them; it only takes a minute: Sign up CodeIgniter - how to catch DB errors? up vote 36 down vote favorite 6 a database error occurred codeigniter Is there a way to make CI throw an exception when it encounters a DB error instead of displaying a message like: A Database Error Occurred Error Number: 1054 Unknown column 'foo' in 'where clause' SELECT * FROM (`FooBar`) WHERE `foo` = '1' NOTE: I only want this to happen in one controller. In the other controllers, I'm happy for it to display the DB error messages. php codeigniter share|improve
Codeigniter Active Record Error Handling
this question edited Oct 20 '11 at 23:29 asked Oct 20 '11 at 23:22 StackOverflowNewbie 9,96467186337 add a comment| 7 Answers 7 active oldest votes up vote 34 down vote accepted Try these CI functions $this->db->_error_message(); (mysql_error equivalent) $this->db->_error_number(); (mysql_errno equivalent) share|improve this answer edited Oct 21 '11 at 4:22 answered Oct 21 '11 at 4:17 Oskenso Kashi 51147 3 And when the queries are dinamically created, $this->db->last_query() useful too. –uzsolt Oct 21 '11 at 15:17 5 How do these prevent the messages from displaying? –StackOverflowNewbie Oct 21 '11 at 21:36 7 You must turn debug off for database in config/database.php -> $db['default']['db_debug'] = FALSE; –decebal Dec 12 '12 at 12:47 8 Why is not possible to use Exception instead :-( ? –Thomas Decaux Mar 8 '13 at 11:39 16 These methods have been removed in CodeIgniter version 3. Use $this->db->error() instead. (see codeigniter.com/user_guide/database/…) –mxgr Jul 26 '15 at 11:17 | show 2 more comments up vote 15 down vote Maybe this: $db_debug = $this->db->db_debug; //save setting $this->db->db_debug = FALSE; //disable debugging for queries $result = $this->db->query($sql); //run query //check for errors, etc $this->db->db_debug = $db_debug; //restore setting share|improve this answer answered Jun 18 '13 at 6:15 RayJ 31133 add a comment| up v
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 Stack Overflow the company Business Learn more about hiring developers or
Codeigniter Try Catch
posting ads with us Stack Overflow Questions Jobs Documentation Tags Users Badges Ask Question x Dismiss codeigniter get mysql error Join the Stack Overflow Community Stack Overflow is a community of 4.7 million programmers, just like you, helping each other. Join them; it only codeigniter insert error-handling takes a minute: Sign up Database Error Handling problem in CodeIngniter up vote 0 down vote favorite 2 I use CodeIgniter as my web application framework. I used a simple Try/Catch and I sent a sample value to test it, http://stackoverflow.com/questions/7843406/codeigniter-how-to-catch-db-errors and it failed! I know I can use $this->db->escape() function to solve my data problem but I just want to know: Why TRY/CATCH can not catch this error! Controler code: $this->load->model('user_model'); $result = $this->user_model->test_user("tes'ti"); Model code: function test_user($username){ try { $query_str = "SELECT * FROM tbl_user WHERE username = '".$username."'"; $result = $this->db->query($query_str); return $result; } catch (Exception $e) { return; } } Output: A Database Error Occurred Error Number: 1064 You have an error in your SQL syntax; check the http://stackoverflow.com/questions/3418254/database-error-handling-problem-in-codeingniter manual that corresponds to your MySQL server version for the right syntax to use near 'ti'' at line 1 SELECT * FROM tbl_user WHERE username = 'tes'ti' Let me know, where I made a mistake, if I did! Thank you for your time and helping others. ;) php mysql codeigniter error-handling try-catch share|improve this question edited Aug 6 '10 at 17:48 asked Aug 5 '10 at 19:08 Monica 3692717 add a comment| 4 Answers 4 active oldest votes up vote 2 down vote accepted You need to throw an exception if there was some mysql error: try { $query_str = "SELECT * FROM tbl_user WHERE username = '".$username."'"; $result = $this->db->query($query_str); if (!$result) { throw new Exception('error in query'); return false; } return $result; } catch (Exception $e) { return; } share|improve this answer answered Aug 5 '10 at 19:14 Sarfraz 235k51386491 Thanks Sarfraz, I did your recommendations but it failed again! do you have more ideas?! –Monica Aug 5 '10 at 19:26 @Monica: What error/output came about when you did this? –Sarfraz Aug 5 '10 at 19:31 I've mentioned the error in Output part of my question... ;) –Monica Aug 5 '10 at 19:34 1 @Monica: Then i am pretty much sure it is handled/done so by the query function $this->db->query. If you try the same code with simple mysql_query command for example in a test page, it should catch it. –
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 research database error 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. The CI catch database error 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