Error Handling Mysql Query Php
Contents |
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 php mssql query error handling Installed as an Apache module Session Security Filesystem Security Database php mysql error handling try catch Security Error Reporting Using Register Globals User Submitted Data Magic Quotes Hiding PHP Keeping Current Features HTTP
Php Mysql Insert Error Handling
authentication with PHP Cookies Sessions Dealing with XForms Handling file uploads Using remote files Connection handling Persistent Database Connections Safe Mode Command line usage Garbage Collection DTrace
Php Mysql Pdo Error Handling
Dynamic Tracing Function Reference Affecting PHP's Behaviour Audio Formats Manipulation Authentication Services Command Line Specific Extensions Compression and 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 error handling in mysql stored procedure 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 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 mysql_real_escape_string » « mysql_ping PHP Manual Function Reference Database Extensions Vendor Specific Database Extensions MySQL MySQL (Original) MySQL Functions Change language: English Brazilian Portuguese Chinese (Simplified) French German Japanese Korean Romanian Russian Spanish Turkish Other Edit Report a Bug mysql_query (PHP 4, PHP 5)mysql_query — Send a MySQL query Warning This extension was deprecated in PHP 5.5.0, and it was removed in PHP 7.0.0. Instead, the MySQLi or PDO_MySQL extension should be used. See also MySQL: choosing an API guide and related FAQ for more information. Alternatives to this
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
Php Sql Error
company Business Learn more about hiring developers or posting ads with us Stack Overflow php mysqli error Questions Jobs Documentation Tags Users Badges Ask Question x Dismiss Join the Stack Overflow Community Stack Overflow is a community of 4.7 mysql error php million programmers, just like you, helping each other. Join them; it only takes a minute: Sign up PHP MySQL error handling up vote 2 down vote favorite 1 Simple question (I hope). At the moment I http://php.net/manual/en/function.mysql-query.php am using the following code: mysql_query($sql) or header("Location: /error"); To prevent the rest of the script running I need to add exit; or die(). Is there any way to do that using the inline approach as above or do I need to use: $result = mysql_query($sql); if (!result) { header("Location: /error"); exit(); } Thanks php mysql share|improve this question edited Aug 28 '13 at 20:11 hdvianna 337211 asked Nov 8 '10 at 16:04 http://stackoverflow.com/questions/4125559/php-mysql-error-handling lethalMango 2,23583673 add a comment| 4 Answers 4 active oldest votes up vote 10 down vote accepted How about: function customDie($location) { header('Location: ' . $location); exit(); } mysql_query($sql) or customDie("/error"); share|improve this answer answered Nov 8 '10 at 16:07 Fosco 27.2k45593 Never thought about doing that, great idea. Thanks! –lethalMango Nov 8 '10 at 16:18 add a comment| up vote 5 down vote If you insist on doing things this way it is better to make a custom query method which handles all this. Something like function custom_mysql_query($query) { $doDebug=true; // Set to true when developing and false when you are deploying for real. $result=mysql_query($query); if(!$result) { if($doDebug) { // We are debugging so show some nice error output echo "Query failed\n
$query\n"; echo mysql_error(); // (Is that not the name) } else { // Might want an error message to the user here. } exit(); } } Then just call custom_mysql_query instead of mysql_query then you will always die if a query fails and if $debug is true, you will also get the query which failed and the database error. But really: You should NEVER use mysql_query or functions which call it(Such as the one I just wrote). It is far too unsafe to ever be used. (Far too difficult to avoid sq
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 http://stackoverflow.com/questions/5019160/catching-mysql-query-error more about hiring developers or posting ads 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 4.7 million programmers, just like you, helping each other. Join them; it only takes a minute: Sign up Catching mysql_query error up vote 4 down vote favorite I am trying to stop the mysql_query error from being output onto my form. Currently if there error handling is no location found, I receive the error "Warning: mysql_result() [function.mysql-result]: Unable to jump to row 0 on MySQL result index 11" I am trying to catch this error, and instead assign the $location variable to not found. My code for attempting this is below, what am I doing wrong? Thanks! $query3 = "SELECT `location` FROM location WHERE vin = '$vin' LIMIT 1"; $result3 = mysql_query($query3); if (!mysql_result($result3,0)) { $location = "Not found"; } else $location error handling mysql = mysql_result($result3,0,0); php mysql share|improve this question asked Feb 16 '11 at 16:30 cds5059 55114 add a comment| 5 Answers 5 active oldest votes up vote 5 down vote accepted mysql_result() generally shouldn't be used. You'd be better off with something like: $result3 = mysql_query($query3) or die(mysql_error()); if (mysql_numrows($result3) == 0) then $location = "not found"; } else { $row = mysql_fetch_array($result3); $location = $row[0]; } Your error is caused by the fact that the query returned no rows - e.g. nothing matched. You then tried to retrieve the first field in the first row of that result set, a row which doesn't exist. Checking the number of returned rows with mysql_numrows() is safer, as that works whether the query found nothing or a bajillion rows. share|improve this answer edited Feb 16 '11 at 16:44 answered Feb 16 '11 at 16:35 Marc B 272k23231350 This should be the proper way to solve this. Don't hide your problems, solve them instead ;) –Max Kielland Feb 16 '11 at 16:38 Worked great, thank you! –cds5059 Feb 16 '11 at 16:46 add a comment| up vote 0 down vote You should look into how to set your error and warning levels in php ini - usually you want a s little output on prod as possible. http://www.php.net/manual/en/errorfunc.configuration.php#ini.error-reporting However, here, the code that would generate that