shane18 Posted December 16, 2009 Share Posted December 16, 2009 Can someone explain to me the best way to do this... Lets say... I have a post comment page and the query fails... how can i stop that error from showing up.. and make it where i can create my own little error message.... i may sound like a noob but i been programming php for about 6 years off and on.. but i never made a site that would be actually used by a lot of people and never handled errors in a good way... Please and thanks... if u can create a simple good example of a query fail and how to handle it that would be AWESOME! cuz i can program the entire social site im making.... but i need to add in the error handling before i release it -Important note.... if i have a query to lookup data for the next query.. how do i make sure if a error is triggered for the first query the second won't post a blank/miss-data entry? Quote Link to comment https://forums.phpfreaks.com/topic/185311-mysql-error-handling/ Share on other sites More sharing options...
oni-kun Posted December 16, 2009 Share Posted December 16, 2009 Can someone explain to me the best way to do this... Lets say... I have a post comment page and the query fails... how can i stop that error from showing up.. and make it where i can create my own little error message.... i may sound like a noob but i been programming php for about 6 years off and on.. but i never made a site that would be actually used by a lot of people and never handled errors in a good way... Please and thanks... if u can create a simple good example of a query fail and how to handle it that would be AWESOME! cuz i can program the entire social site im making.... but i need to add in the error handling before i release it -Important note.... if i have a query to lookup data for the next query.. how do i make sure if a error is triggered for the first query the second won't post a blank/miss-data entry? What is the code for your query? You should check if all the entries are filled before you ever insert it into your database. Use mysql_real_escape_string on all fields entering into your database to sanitize your input, and enter: or die('Warning: MySQL has performed an error: ' . mysql_error()); On the same line of your mysql_query().. But it'd be useful to see your code.. Quote Link to comment https://forums.phpfreaks.com/topic/185311-mysql-error-handling/#findComment-978261 Share on other sites More sharing options...
shane18 Posted December 16, 2009 Author Share Posted December 16, 2009 Well what I posted above was a example.... heres my real code.... if($_GET["Page"] == Post_Comment && $_GET["Part"] == 2){ LoginForce($USER_ID, 0); if($USER_ID !== $_POST["profileid"]){ mysql_query("insert into comments (from_id, to_id, content, timestamp) VALUES('$USER_ID','{$_POST["profileid"]}','{$_POST["commentcontent"]}','$TIMESTAMP')"); smooth_redirect("Posting Comment","index.php?Page=Profile&PROFILE_ID={$_POST["profileid"]}"); }else{ error_box("You cannot comment yourself!"); } } thats my post comment code... how can i make it were if a error was triggered by the query i can make my own custom box pop up where i want it too with my own custom message... and stop the default error stuff... Quote Link to comment https://forums.phpfreaks.com/topic/185311-mysql-error-handling/#findComment-978265 Share on other sites More sharing options...
mikesta707 Posted December 16, 2009 Share Posted December 16, 2009 If you want to handle your error's elegantly, I would advise against using the "or die" First of all, you DONT want to show your errors to your users. You want to catch errors (either with exceptions, or clever if statements, or combinations of those) and show a general error message ("Like oops something happened, try to reload"), and right the actual errors to an error log. Quote Link to comment https://forums.phpfreaks.com/topic/185311-mysql-error-handling/#findComment-978267 Share on other sites More sharing options...
shane18 Posted December 16, 2009 Author Share Posted December 16, 2009 Yea, that is what im trying to do!!! is make a general error message... so like if the query fails... i can cancel the mysql default error message....... then let it display my own little message... can u show me a example im lost with the error stuff.. never touched it and i been doing php programming for 6 years... ahah... but i need to figure this out cuz im making a site thats actually gona be used by 100+ people... Quote Link to comment https://forums.phpfreaks.com/topic/185311-mysql-error-handling/#findComment-978269 Share on other sites More sharing options...
shane18 Posted December 16, 2009 Author Share Posted December 16, 2009 Would this be a smart way??? 1. At the top of the page... turn off all errors using error_reporting(0); 2. Use a if.... if($DB->error){ to display the error and etc? Quote Link to comment https://forums.phpfreaks.com/topic/185311-mysql-error-handling/#findComment-978277 Share on other sites More sharing options...
shane18 Posted December 16, 2009 Author Share Posted December 16, 2009 Bump Quote Link to comment https://forums.phpfreaks.com/topic/185311-mysql-error-handling/#findComment-978479 Share on other sites More sharing options...
PFMaBiSmAd Posted December 16, 2009 Share Posted December 16, 2009 You always want error_reporting to be at least E_ALL (so that you can log all errors, such as when a legitimate visitor (or a hacker) feeds your code unexpected data that your validation logic does not catch.) However, on a development system, you want display_errors to be ON and on a live server you want display_errors to be OFF and you want log_errors to be ON. For your actual error checking, error reporting, and error recovery logic in your code, in its' most basic form - if($result = mysql_query($query)){ // the query executed without error and returned a result resource (SELECT, SHOW, DESCRIBE, EXPLAIN, ... query) or a TRUE value (INSERT, UPDATE, DELETE, DROP, ... query) // your code to process the results of the query here ... } else { // the query failed to execute and returned a FALSE value, usually a syntax error or a problem with the database/table // code to execute when the query failed with an error ... // do any application level error reporting/logging - $application_message = "Query failed: $query<br />Mysql error: " . mysql_error(); // produce message trigger_error($application_message,E_USER_NOTICE); // trigger a NOTICE level error and optionally output/log (depending on error_reporting/display_errors/log_errors settings) // output a user error message echo "Due to an error, the application cannot access data at this time<br />"; } // the remainder of the code on your page that is not dependent on the above query working or not working ... Edit: You should set any error_reporting, display_errors, and log_errors settings globally in the master php.ini, a .htaccess file, or a local php.ini so that you don't need to waste your time continually altering your source code to change the settings in them. Those cases where you see us recommend adding lines to scripts to set those settings are for the "my code is not working and it is not telling me why" posts where it is likely the OP is trying to debug some basic code problems (not finished working code put on to a live server) on a server where those settings are not set to give any immediate feedback. Quote Link to comment https://forums.phpfreaks.com/topic/185311-mysql-error-handling/#findComment-978487 Share on other sites More sharing options...
shane18 Posted December 16, 2009 Author Share Posted December 16, 2009 Thanks Quote Link to comment https://forums.phpfreaks.com/topic/185311-mysql-error-handling/#findComment-978680 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.