php - mysqli or die, does it have to die?
Get the solution ↓↓↓If I use a bit of code like this:
$update_result = mysqli_query( $link , $sql_update_login ) or die ('Unable to execute query. '. mysqli_error($link));
Does it have to die or can you put a different query afterwards? Like a predetermined function that writes a log of the error to another table? Such as:
$update_result = mysqli_query( $link , $sql_update_login ) or function('$query, $error);
What are the other options after 'or'? I haven't found it in the documentation, any clues are appreciated.
Answer
Solution:
Does it have to die
Quite contrary, it shouldn'tor die()
ever.
PHP is a language of bad heredity. Very bad heredity. Andor die()
with an error message being one of the worst rudiments:
- die throws an error message out, revealing some system internals to a potential attacker
- it is confusing innocent users with strange messages and leaving them no interface to work with, so they'd likely just drop out.
- it kills the script in the middle, so it may cause torn design (or no design at all) shown (i.e. an incomplete render of the page the user requested)
- killing the script irrecoverably. While thrown exception can be caught and gracefully handled
die()
gives you no hint of the place where the error occurred. And in a relatively big application it will be quite a pain to find.
So, never usedie()
with MySQL errors, even for the temporary debugging: there are better ways.
Instead of manually checking for the error, just configure mysqli to throw exceptions on error, by adding the following line to your connection code
mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);
and after that just write every mysqli command as is, without anyor die
or anything else:
$result = mysqli_query($link, $sql);
This code will throw an exception in case of error and thus you will always be informed of every problem without a single line of extra code.
A more detailed explanation on how to make your error reporting production ready, uniform and overall sensible while making your code much cleaner, you can find in my article on PHP error reporting.
Answer
Solution:
or
is just an operator (very similar to).
Theor die()
syntax works becauseor
short-circuits, which means that if the first statement is true,True or X
will always be true, soX
isn't evaluated and your script doesn'tdie
.
Answer
Solution:
Yes, you can provide a different function after the (or). I have tested the following:
mysqli_query($sel_db,'what!') or some_func(mysqli_error($sel_db));
function some_func($str) {
die("ERROR: ".$str);
}
Answer
Solution:
It doesn't have to bedie()
specifically, but it needs to be something that'll make the script halt by callingexit()
ordie()
, or something that throws an exception. Otherwise, the script will continue with the return value of that function (which is probably either null or some sort of junk) in$update_result
, which will almost certainly cause problems.
Share solution ↓
Additional Information:
Link To Answer People are also looking for solutions of the problem: using $this when not in object context
Didn't find the answer?
Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.
Similar questions
Find the answer in similar questions on our website.
Write quick answer
Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.