Jump to content

[SOLVED] benefits of die()


ahs10

Recommended Posts

i have created a function that is called in replace of die, usually after a mysql_query() attempt, but sometimes in an else statement. i don't however use this function to replace die() when it's used because of denied user permissions. anyweezer, this function is just error reporting that's easier on the eye, emailing me the __FILE__, __LINE__, and mysql_error, and opening up a form in a new window that allows the user to associate their name, email address, and any notes to the error report that was just emailed to me.

 

so with that setup, the form is opened in a new window, so the original error causing page is underneath it and still open. what i just realized to is that the script wasn't killed either. now i could add die() to my function, but do i need too?

 

are there instances where continuing to run a script after an mysql_error is bad? is there a reason why "$result = mysql_query($query) or die(mysql_error())" is one of the most common lines in php? like i said, i don't use this function to replace die() when it's denying a user access, there's usually a mysql_error to go along with it, and if not it's in a bizarre else statement that will probably never get executed.

Link to comment
Share on other sites

thanks for this information, it helps me ask a better question :)

 

on this page, someone posts.....

 

"error_fatal(E_ALL^E_NOTICE); // will die on any error except E_NOTICE"

 

why do you want to do that?  why is running the script after E_NOTICE occurs alright?  why is running the script after any other bad?

 

 

 

Link to comment
Share on other sites

People often work with error reporting set to ignore notices. You get notices for undefined variables/constants amongst other things. Because they occur with this sort of problem, often the script can continue to run - though it may not always run as expected. For example, this:

 

<?php
$array  = array('key' =>'some value');
echo $array[key];
?>

 

Would produce an undefined constant notice. However, PHP can continue to execute the script, because it assumes you meant to enclose the word key in quotes.

 

Setting error reporting to include notices is a good way of finding bugs though. If you made a typo with a variable name somewhere, it'll help you find it.

Link to comment
Share on other sites

thank yall for your help, but i don't think i'm explaining myself well here at all.

 

i know what the ^ symbol does, i know that custom error reporting is a common practice, i know that the script can continue to run after there was an error.  i'd like to know the danger in it running after an error.

 

let's say for example that you....

 

$query = "SELECT * table";

$result = mysql_query($query);  // this query fails, giving an error

$var = mysql_result($result, 0); // since the query failed, i'm sure this would kick an error too, irrelevant to my point though that $var doesn't have a value

 

$query2 = "UPDATE different_table SET whatever = ' " . $var . " ' ";

$result2 = mysql_query($query2);  // this query doesn't fail

 

...so with this remedial example, i've updated one table with an incorrect value because i didn't die at my first error.  i'm looking for more examples like this, why it would be bad not to die after an error.

 

Link to comment
Share on other sites

As you've pretty much pointed out, it depends entirely on the script. Bad things could occur if you let a script continue to run when there should be some form of error. Consider the pseudocode:

 

if(person doesn't have any enough money){
//show error
}
//complete purchase

 

Without exiting the script, the purchase is still complete, even though the error is shown.

 

As i said though, wether or not you would run into any problems depends entirely on the script.

Link to comment
Share on other sites

The 'danger' involved in allowing the script to continue depends on the actual problem, so there isn't a clear cut answer to your question. This is also a "programmer's call" situation. Some coders like to pay attention to even the smallest of details, and others just feel that if the script 'works', that's good enough.

 

PhREEEk

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.