Jump to content

MySQL Error Handling


shane18

Recommended Posts

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?

Link to comment
Share on other sites

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..

Link to comment
Share on other sites

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...

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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...

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.