Mysql Error Handling 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
Codeigniter Active Record Error Handling
more about Stack Overflow the company Business Learn more about hiring developers or codeigniter query error handling posting ads with us Stack Overflow Questions Jobs Documentation Tags Users Badges Ask Question x Dismiss Join the Stack Overflow
Codeigniter Try Catch
Community Stack Overflow is a community of 6.2 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 codeigniter get mysql error 36 down vote favorite 6 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 _error_message() the DB error messages. php codeigniter share|improve this question edited Oct 20 '11 at 23:29 asked Oct 20 '11 at 23:22 StackOverflowNewbie 10k67188338 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 17 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
here for a quick overview of the site Help Center Detailed answers to any questions you might have
Call To Undefined Method Ci_db_mysql_driver::_error_message()
Meta Discuss the workings and policies of this site About Us codeigniter database query error handling Learn more about Stack Overflow the company Business Learn more about hiring developers or posting ads
Codeigniter $this- Db- Error_number ()
with us Stack Overflow Questions Jobs Documentation Tags Users Badges Ask Question x Dismiss Join the Stack Overflow Community Stack Overflow is a community of 6.2 million programmers, http://stackoverflow.com/questions/7843406/codeigniter-how-to-catch-db-errors just like you, helping each 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 http://stackoverflow.com/questions/17923382/how-to-catch-db-errors-in-codeigniter-php by following statement one can catch the 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 66741730 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 sp
full version with proper formatting. RastoStric02-23-2015, 04:03 http://forum.codeigniter.com/archive/index.php?thread-1255.html AM I am aware of the discussions on this topic in the CI forum, StackOverflow and elsewhere. I tried to do my research carefully but it seems to me, that I did not find a proper answer. The question error handling is how to handle database transaction errors and not to 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 mo