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 Stack Overflow the this db _error_message company Business Learn more about hiring developers or posting ads with us Stack Overflow Questions
Codeigniter Active Record Error Handling
Jobs Documentation Tags Users Badges Ask Question x Dismiss Join the Stack Overflow Community Stack Overflow is a community of 4.7 million codeigniter query error handling programmers, just like you, helping each other. Join them; it only 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.
Codeigniter Try Catch
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 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) codeigniter get mysql error { 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; } share|improve this answer answered Aug 5 '10 at 19:14 Sarfraz 236k51386491 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
full version with proper formatting. RastoStric02-23-2015,
Codeigniter Insert Error-handling
04:03 AM I am aware of the
Codeigniter Error Handling
discussions on this topic in the CI forum, StackOverflow and elsewhere. codeigniter $this- db- error_number () I tried to do my research carefully but it seems to me, that I did not find a proper http://stackoverflow.com/questions/3418254/database-error-handling-problem-in-codeingniter answer. The question 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();
http://forum.codeigniter.com/archive/index.php?thread-1255.html />$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 handlin
Generators References Explained Predefined Variables Predefined Exceptions Predefined Interfaces and Classes Context options and parameters Supported Protocols and Wrappers Security Introduction General considerations Installed as CGI binary Installed as an Apache module Session Security Filesystem http://php.net/manual/en/pdo.error-handling.php Security Database Security Error Reporting Using Register Globals User Submitted Data Magic Quotes Hiding PHP Keeping Current Features HTTP authentication with PHP Cookies Sessions Dealing with XForms Handling file uploads Using remote https://codedump.io/share/E07YJraQ2aTj/1/codeigniter---how-to-catch-db-errors files Connection handling Persistent Database Connections Safe Mode Command line usage Garbage Collection DTrace Dynamic Tracing Function Reference Affecting PHP's Behaviour Audio Formats Manipulation Authentication Services Command Line Specific Extensions Compression and error handling Archive Extensions Credit Card Processing Cryptography Extensions Database Extensions Date and Time Related Extensions File System Related Extensions Human Language and Character Encoding Support Image Processing and Generation Mail Related Extensions Mathematical Extensions Non-Text MIME Output Process Control Extensions Other Basic Extensions Other Services Search Engine Extensions Server Specific Extensions Session Extensions Text Processing Variable and Type Related Extensions Web Services Windows Only catch database error Extensions XML Manipulation Keyboard Shortcuts? This help j Next menu item k Previous menu item g p Previous man page g n Next man page G Scroll to bottom g g Scroll to top g h Goto homepage g s Goto search(current page) / Focus search box Large Objects (LOBs) » « Prepared statements and stored procedures PHP Manual Function Reference Database Extensions Abstraction Layers PDO Change language: English Brazilian Portuguese Chinese (Simplified) French German Japanese Korean Romanian Russian Spanish Turkish Other Edit Report a Bug Errors and error handling PDO offers you a choice of 3 different error handling strategies, to fit your style of application development. PDO::ERRMODE_SILENT This is the default mode. PDO will simply set the error code for you to inspect using the PDO::errorCode() and PDO::errorInfo() methods on both the statement and database objects; if the error resulted from a call on a statement object, you would invoke the PDOStatement::errorCode() or PDOStatement::errorInfo() method on that object. If the error resulted from a call on the database object, you would invoke those methods on the database object instead. PDO::ERRMODE_WARNING In addition to setting the error co
Gherkin Git Go Groovy Haml Handlebars Haskell HTML HTTP Ini iOS Jade Java Javascript jQuery JSON Julia Keyman LaTeX Linux Less LOLCODE Makefile Markdown MATLAB MySQL NASM Node.js NSIS Objective-C Pascal Perl PHP PHP Extras PowerShell Python R React JSX reST (reStructuredText) Rip Ruby Rust SAS Sass (Sass) Sass (Scss) Scala Scheme Smalltalk Smarty SQL Stylus Swift Twig TypeScript Vb.net VHDL Wiki markup YAML Other Search StackOverflowNewbie - 8 months ago 130x PHP Question CodeIgniter - how to catch DB errors? 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. Oskenso Kashi Answer Email {} Share Try these CI functions $this->db->_error_message(); (mysql_error equivalent) $this->db->_error_number(); (mysql_errno equivalent) Source (Stackoverflow) http://stackoverflow.com/questions/7843406/codeigniter-how-to-catch-db-errors Comments Please enable JavaScript to view the comments powered by Disqus. × Email codedump link for CodeIgniter - how to catch DB errors? Email has been send. To emailaddress: To name: From name: Extra information in the email body (optional): Email: I am sending you the codedump of CodeIgniter - how to catch DB errors? that you can see here: https://codedump.io/share/E07YJraQ2aTj/1 Close Send email Share Sign up Sign up with GitHub Email: Displayname Password: Repeat password: Tags php codeigniter Latest added Bash script to check diskspace Save toggle state to local storage trabalhinho Firewalld open ssh port for specific ip address Trabalho Terms & services -Privacy -Rss Sign in -Sign up For programmers by programmers Tag v2.0.1