Catch Codeigniter 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 codeigniter catch db error and policies of this site About Us Learn more about Stack Overflow codeigniter database error handling the company Business Learn more about hiring developers or posting ads with us Stack Overflow Questions Jobs Documentation codeigniter a database error occurred 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. Join them; it codeigniter database error message 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 error. $this->db->_error_message(); But I cannot figure
Codeigniter Active Record Error Handling
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 answer answered Jul 29 '13 at 12:02 Erman Belegu 2,7471431 add a comment| up
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
Codeigniter Query Error Handling
this site About Us Learn more about Stack Overflow the company Business codeigniter try catch Learn more about hiring developers or posting ads with us Stack Overflow Questions Jobs Documentation Tags Users Badges Ask codeigniter get mysql error Question x Dismiss 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 takes a minute: Sign http://stackoverflow.com/questions/17923382/how-to-catch-db-errors-in-codeigniter-php 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, 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 http://stackoverflow.com/questions/3418254/database-error-handling-problem-in-codeingniter 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 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; }
production environment and notify about it to developers, also at the same http://thecancerus.com/how-to-catch-php-fatal-error-in-codeigniter/ time, showing our beautiful fail whale page to user instead of ugly error or a white screen of death. Here I will show you how to do this https://github.com/jamierumbelow/codeigniter-base-model/issues/52 in CodeIgniter, though credit for this idea goes to hipertracker. First of all you will need to setup a hook, so update your hooks.php file with following code $hook['pre_system'][] database error = array( 'class' => 'PHPFatalError', 'function' => 'setHandler', 'filename' => 'PHPFatalError.php', 'filepath' => 'hooks' ); Now put the code shown below in PHPFatalError.php file in your applications hooks folder. This is the simplifed version of what I have done, so please update handleShutdown function as per your needs. Check out PHPCamp a place to share news, views codeigniter database error and articles that are useful to PHP community. Posted in: how too? Tagged with: codeigniter, php, php fatal errors. Post navigation ← Get An Introduction To Functional Programming At TechWeekend 5 Aspiring WordPress Developers Read These Articles First → 5 thoughts on “How To Catch PHP Fatal Error In CodeIgniter” Pedram says: February 15, 2011 at 11:44 pm I'm sorry to interrupt here but this post's comments are closed http://amiworks.co.in/talk/how-to-remove-new-folderexe-or-regsvrexr-or-autoruninf-virus/ I can't delete "svchost .exe" it says: ---------------- Cannot delete svchost: Access is denied Make sure the disk is not full or write-protected and that the file is not currently in use. ---------------- what should I do? many thanks Sparrow says: July 16, 2011 at 8:12 pm where are function load_page? I have error, PHP Fatal error: Call to undefined function load_page() sushma kale says: July 4, 2012 at 4:15 pm can you please help, i m having php version 5.1.6 in which error_get_last() function is not available so i m unable to use the given code G
Support Search GitHub This repository Watch 158 Star 916 Fork 471 jamierumbelow/codeigniter-base-model Code Issues 75 Pull requests 35 Projects 0 Pulse Graphs New issue Request: Catching errors #52 Open peterdenk opened this Issue Sep 16, 2012 · 22 comments Projects None yet Labels None yet Milestone No milestone Assignees No one assigned 8 participants peterdenk commented Sep 16, 2012 Many lines of code would be saved if we could catch simple errors with the base model and have an error message echoed and/or logged. You know all those infrequent errors that you want to check for but don't want to make a pretty user friendly interface for handling since it's broke and any how only the man in charge can fix it. Like this: ->on_error('Foobar missing.') ->get_all(...); ->on_error('Multiple Foobars matching criteria.') ->get_one(...); Could even have a default error message so that just this would do the trick: ->on_error() ->get_all(...); Brilliant or simply stupid? Owner jamierumbelow commented Sep 16, 2012 I've actually been thinking about this for a while. Rather than return errors like described above, I'd much rather throw exceptions: try { $book = $this->book->get(1); } catch (MY_Model\RecordNotFound $e) { show_404(); } catch (MY_Model\SQLError $e) { log_message('error', $e->getMessage()); $book = FALSE; } The reason I haven't implemented it yet is because I'm worried it will be a culture shock for a lot of CI devs (CI isn't exactly an exception-conducive environment). The benefits of using exceptions are many: errors can be selectively ignored, easily logged, caught and re-thrown with new messages, reported to other parts of the application, not to mention all the extra context you get (stack traces). I'd certainly favour an exception-based system, but what does the community think? facultymatt commented Sep 16, 2012 @jamierumbelow I'd be interested in seeing how the exceptions approach would play out. Especially after reading your book on API design, I'm beginning to see the power in throwing exceptions. It might be helpful to see this approach in more detail, perhaps in a feature branch, so that the community could better understand how this method would effect things. Personally, my only concern with this approach is this seems like it would add a ton of lines to the code - adding try/ catch/ catch lines to many of the model functions (ie: get, get_all, get_by, get_many_by, insert, update, delete). And this could, as you pointed out, be a bit of a shock. It could also make the code harder to follow. peterdenk commented Sep 16, 2012 Throwing an