Redirect on "or die"
Moderator: General Moderators
Redirect on "or die"
Instead of or die(mysql_error()); is there a way I can send my or die to a friendly page that tells the user "Something went wrong. Please wait a few minutes and try again."? Can you show me a basic example how to redirect to a MySQL error page like this?
Thank you.
Thank you.
Re: Redirect on "or die"
Code: Select all
$result = mysql_query($query);
if($result === false) {
header("Location: error.php");
} else {
// process query results
}Re: Redirect on "or die"
Or
I personally have a redirect function that calls die() itself...so if you want to use less code, then you can do that 
Code: Select all
mysql_query('...') or your_header_function('Location: page.php');Re: Redirect on "or die"
I don't understand why that makes it a "bad habbit"...
Re: Redirect on "or die"
Re: Redirect on "or die"
that is one thing, another is that die() has no part in error handling. It might be useful for debugging, but you can debug without making the habit of killing your scripts on every error.
Re: Redirect on "or die"
yeah, I agree that die() shouldn't be used for handling errors. But I don't understand why using "or" is bad...
Re: Redirect on "or die"
Can you show an example code snippet where using OR is the best solution?jackpf wrote:yeah, I agree that die() shouldn't be used for handling errors. But I don't understand why using "or" is bad...
Re: Redirect on "or die"
Code: Select all
$fh = fopen('somefile.txt') or trigger_error('Could not open this file...', E_USER_ERROR);Re: Redirect on "or die"
It seems to me that there would be code following that line which would depend on the file handle. The Errors would cascade.
Maybe:
I don't know, something about it seems "off", but it could be because I have never used OR in that context.
Maybe:
Code: Select all
$fh = fopen('somefile.txt') or throw new Exception('Could not open this file...');
Re: Redirect on "or die"
But the code below wouldn't even be executed since I called trigger_error(), from which I would stop the script from parsing and show the user and error 
I honestly don't see anything wrong with it. I think it's a great operator for reducing the amount of space code takes up.
I honestly don't see anything wrong with it. I think it's a great operator for reducing the amount of space code takes up.
Re: Redirect on "or die"
Less lines do not mean better code.
The code is better when it's easy to understand it. or is not obvious operator; because of this sometime it is used in boolean expression when || should be used, or as in the posted code in that topic - in the return expression.
Additionally, the "or" error handling does not allow a second line of code, and this is not good. What if fwrite failed and you want to close the handler before throwing exception (I bet the ones who use 'or die' or 'or trigger_error' do not care about opened handles, memory leaks and so on)
The code is better when it's easy to understand it. or is not obvious operator; because of this sometime it is used in boolean expression when || should be used, or as in the posted code in that topic - in the return expression.
Additionally, the "or" error handling does not allow a second line of code, and this is not good. What if fwrite failed and you want to close the handler before throwing exception (I bet the ones who use 'or die' or 'or trigger_error' do not care about opened handles, memory leaks and so on)
Re: Redirect on "or die"
I find it easy to read.
Also, in most cases all you want to do is call your own user defined error function. The only time I ever use it is when I want to execute one function if another returns false. In all other cases I use the boolean "or" (||) operator.
And I doubt having a file handle open for that extra milli-second or however long PHP takes to echo an error message would be too much of a problem...
Also, in most cases all you want to do is call your own user defined error function. The only time I ever use it is when I want to execute one function if another returns false. In all other cases I use the boolean "or" (||) operator.
And I doubt having a file handle open for that extra milli-second or however long PHP takes to echo an error message would be too much of a problem...
Re: Redirect on "or die"
personally, I've never seen the OR operator in production code, only in basic tutorials. I can't really think of a good use for it either.
And as I've written previously, you don't want your script dying whenever an error occurs, that's not error handling. So regardless if you throw a fatal error, use die() inline or in a function, that's not good practice. What Darhazer referred to - performing some cleanup before directing to an error page is the common practice.
And as I've written previously, you don't want your script dying whenever an error occurs, that's not error handling. So regardless if you throw a fatal error, use die() inline or in a function, that's not good practice. What Darhazer referred to - performing some cleanup before directing to an error page is the common practice.