Catch 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 about Stack Overflow the company Business Learn codeigniter catch database error more about hiring developers or posting ads with us Stack Overflow Questions Jobs Documentation Tags
Codeigniter Catch Db Error
Users Badges Ask Question x Dismiss Join the Stack Overflow Community Stack Overflow is a community of 4.7 million programmers, just like you, codeigniter try catch database error 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 Is there a way to make CI throw an exception when it codeigniter handle database error 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 this question edited Oct 20 '11 at 23:29 asked Oct 20 '11 at 23:22 StackOverflowNewbie 9,95967186337 add a comment|
Validation Error In Codeigniter
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 15 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 14 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 30133 add a comment| up vote 12 down vote In Codeigniter 3.0 (CI3), all you have to do is $this->db->error() If you need to get the last error that has occured, the error() method will return an array containing its code and m
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 404 error in codeigniter Learn more about Stack Overflow the company Business Learn more about hiring developers display error in codeigniter or 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 4.7 million programmers, just like you, helping each other. Join them; it only takes a minute: Sign up Can't catch my mysql errors up vote 0 http://stackoverflow.com/questions/7843406/codeigniter-how-to-catch-db-errors down vote favorite I am using codeigniter for my server side in php. I set my email field UNIQUE on my Users table. The problem is that whatever I tried I can't catch the error mysql generated when trying to insert a duplicate email. What i tried inside my model: function insert($arr) { $query= $this->CI->db->insert('user', $arr); if($query){ return $this->CI->db->insert_id(); } else { $msg = $this->CI-db->_error_message(); return $msg; http://stackoverflow.com/questions/30156422/cant-catch-my-mysql-errors } } The issues goes that everything is fine until I get a duplicate and I actually get NOTHING inside the $msg. I know debug is on from the database config file. mysql codeigniter share|improve this question edited May 11 '15 at 0:27 Shaiful Islam 4,005101938 asked May 10 '15 at 20:39 Dror 3551517 what is your codeigniter version? –Shaiful Islam May 11 '15 at 0:28 3.0.0 - i upgraded from 2.2 –Dror May 11 '15 at 3:23 add a comment| 1 Answer 1 active oldest votes up vote 0 down vote accepted If your database config 'db_debug' => TRUE, your code will exit with showing the error message and you will not able to reach this line $msg = $this->CI-db->_error_message();. So to catch the error message you need to set the. db_debug' => FALSE At CI-2 your above code will work.See more at this question But At CI-3 those function is not available and it will produce php undefined method error. CI-3 has a method display_error. You can check it. My solution: If you want the errors you can get it using this line $msg = $this->db->conn_id->error_list; This will give you the error lists as
Chart Model-View-Controller Architectural Goals Tutorial Static pages News section Create news items Conclusion Contributing to CodeIgniter Writing CodeIgniter Documentation https://www.codeigniter.com/userguide3/database/queries.html Developer's Certificate of Origin 1.1 General Topics CodeIgniter URLs http://thecancerus.com/simple-way-to-add-global-exception-handling-in-codeigniter/ Controllers Reserved Names Views Models Helpers Using CodeIgniter Libraries Creating Libraries Using CodeIgniter Drivers Creating Drivers Creating Core System Classes Creating Ancillary Classes Hooks - Extending the Framework Core Auto-loading Resources Common Functions Compatibility Functions URI database error Routing Error Handling Caching Profiling Your Application Running via the CLI Managing your Applications Handling Multiple Environments Alternate PHP Syntax for View Files Security PHP Style Guide Libraries Benchmarking Class Caching Driver Calendaring Class Shopping Cart Class Config Class Email Class Encrypt Class Encryption Library File error in codeigniter Uploading Class Form Validation FTP Class Image Manipulation Class Input Class Javascript Class Language Class Loader Class Migrations Class Output Class Pagination Class Template Parser Class Security Class Session Library HTML Table Class Trackback Class Typography Class Unit Testing Class URI Class User Agent Class XML-RPC and XML-RPC Server Classes Zip Encoding Class Database Reference Quick Start: Usage Examples Database Configuration Connecting to a Database Running Queries Generating Query Results Query Helper Functions Query Builder Class Transactions Getting MetaData Custom Function Calls Query Caching Database Manipulation with Database Forge Database Utilities Class Database Driver Reference Helpers Array Helper CAPTCHA Helper Cookie Helper Date Helper Directory Helper Download Helper Email Helper File Helper Form Helper HTML Helper Inflector Helper Language Helper Number Helper Path Helper Security Helper Smiley Helper String Helper Text Helper Typography He
for us to know. The idea was that whenever such an exception occur on production we should send an email to developers mailing list so that someone can investigate it. As usual I did a quick google search and i found two forum posts in CodeIgniter and one on stackoverflow, but they all fall short as CodeIgniter does not set’s any default exception handlers they way it sets the native error handler. So here is a quick tutorial on how you can do that. First of all you need to setup a hook, so put following code in hook.php file in config folder. $hook['pre_controller'][] = array( 'class' => 'ExceptionHook', 'function' => 'SetExceptionHandler', 'filename' => 'ExceptionHook.php', 'filepath' => 'hooks' ); Now I am using pre_controller hook as I wanted to use $CI object which is available at this stage. Now put the code shown below in the file named ExceptionHook.php in your application’s hooks folder. Also if you need to capture and email native PHP errors, you can do so by extending the Exceptions library as shown in the code below. While I have used the simple PHP mail function in the example above, you can use CI's mail library as well. If you have any doubts feel free to ask in comments below. Posted in: how too?, php Tagged with: codeigniter, exception handling. Post navigation ← CodeIgniter 2.0 Is Baking Installing PEAR and PHPUnit on WAMP and Windows 7 → 8 thoughts on “Simple Way To Add Global Exception Handling In CodeIgniter” Zack Hovatter says: October 3, 2010 at 10:47 pm Nice. Never thought of this. Pingback: How To Catch PHP Fatal Error In CodeIgniter | am i works? David Mann says: March 13, 2014 at 2:58 pm It's been a while since you posted this, but I've just discovered your idea and I really needed this help. I didn't use all the details of the MY_Exceptions class. I just wanted to convert PHP errors into ErrorExceptions so that I could use try/catch blocks consistently. I come from Java/C# thinking and I don't like old-fashioned errors, I prefer the exception approach. Your tutorial gave me the introduction to hooks that I needed to implement an error handler globally that throws an ErrorException whenever an error occurs. If I need it, I can easily add the email logging using your ideas. Thanks Chirag Adhvaryu says: February 21, 2015 at 5:35 am Hello Sir, I am php d