Error Handling Codeigniter Database
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 more about hiring developers or error handling in codeigniter tutorial posting ads with us Stack Overflow Questions Jobs Documentation Tags Users Badges Ask Question x Dismiss
Codeigniter Catch Database 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 query 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, and codeigniter active record error handling 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 manual
Codeigniter Error Handling
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 236k51387491 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. –Sarfraz Aug
Chart Model-View-Controller Architectural Goals Tutorial Static pages News section Create news items Conclusion Contributing to CodeIgniter Writing CodeIgniter Documentation Developer's Certificate of Origin 1.1 codeigniter try catch General Topics CodeIgniter URLs Controllers Reserved Names Views Models Helpers codeigniter error handling example Using CodeIgniter Libraries Creating Libraries Using CodeIgniter Drivers Creating Drivers Creating Core System Classes Creating Ancillary
Codeigniter Custom Error Page
Classes Hooks - Extending the Framework Core Auto-loading Resources Common Functions Compatibility Functions URI Routing Error Handling Caching Profiling Your Application Running via the CLI http://stackoverflow.com/questions/3418254/database-error-handling-problem-in-codeingniter 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 Uploading Class Form Validation FTP Class Image Manipulation Class Input Class Javascript Class Language Class https://www.codeigniter.com/userguide3/database/queries.html 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 Helper URL Helper XML Helper CodeIgniter Welcome to CodeIgniter Installation Instructions Downloading CodeIgniter Installation Instructions Upgrading From a Previous Version Troubleshooting CodeIgniter Overview Getting Started CodeIgniter at a Glance Supported Features Applicati
full version with proper formatting. RastoStric02-23-2015, 04:03 AM http://forum.codeigniter.com/archive/index.php?thread-1255.html I am aware of the discussions on this topic in the CI forum, StackOverflow and elsewhere. I tried to do https://www.tutorialspoint.com/codeigniter/codeigniter_error_handling.htm my research carefully but it seems to me, that I did not find a proper answer. The question is how error handling 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 more about the DB error (
Installing CodeIgniter CodeIgniter - Application Architecture CodeIgniter - MVC Framework CodeIgniter - Basic Concepts CodeIgniter - Configuration CodeIgniter - Working with Database CodeIgniter - Libraries CodeIgniter - Error Handling CodeIgniter - File Uploading CodeIgniter - Sending Email CodeIgniter - Form Validation CodeIgniter - Session Management CodeIgniter - Flashdata CodeIgniter - Tempdata CodeIgniter - Cookie Management CodeIgniter - Common Functions CodeIgniter - Page Caching CodeIgniter - Page Redirection CodeIgniter - Application Profiling CodeIgniter - Benchmarking CodeIgniter - Adding JS and CSS CodeIgniter - Internationalization CodeIgniter - Security CodeIgniter Useful Resources CodeIgniter - Quick Guide CodeIgniter - Useful Resources CodeIgniter - Discussion Selected Reading Developer's Best Practices Questions and Answers Effective Resume Writing HR Interview Questions Computer Glossary Who is Who CodeIgniter - Error Handling Advertisements Previous Page Next Page Many times, while using application, we come across errors. It is very annoying for the users if the errors are not handled properly. CodeIgniter provides an easy error handling mechanism. You would like the messages to be displayed, when the application is in developing mode rather than in production mode as the error messages can be solved easily at the developing stage. The environment of your application can be changed, by changing the line given below from index.php file. This can be set to anything but normally there are three values (development, test, production) used for this purpose. define('ENVIRONMENT', isset($_SERVER['CI_ENV']) ? $_SERVER['CI_ENV'] : 'development'); Different environment will require different levels of error reporting. By default, development mode will display errors and testing and live mode will hide them. CodeIgniter provides three functions as shown below to handle errors. show_error() function displays errors in HTML format at the top of the screen. Syntax show_error($message, $status_code, $heading = 'An Error Was Encountered') Parameters $message (mixed) − Error message $status_code (int) − HTTP Response status code $heading (string) − Error page heading Return Type mixed show_404() function displays error if you are trying to access a page which does not exist. Syntax show_404($page = '', $log_error = TRUE) Parameters $page (string) – URI string $log_error (bool) – Whether to log the error Return Type void log_message() functi