Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

mysqli or die, does it have to die?

Tags:

php

mysql

mysqli

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.

like image 949
Maelish Avatar asked Mar 10 '13 02:03

Maelish


People also ask

How can I see MySQLi errors?

Just simply add or die(mysqli_error($db)); at the end of your query, this will print the mysqli error.

How do you check SQL query is correct or not in PHP?

Here's Pseudocode of what I would like it to do: <? php //connect user //connect to database //v_query = $_GET['usrinput']; if(validate v_query == true){ echo "This query can be executed"; } else{ echo "This query can't be executed because the table does not exist."; } //disconnect ?> Something like this.

What is Mysqli_error?

Definition and Usage The error / mysqli_error() function returns the last error description for the most recent function call, if any.

How can I get SQL error in PHP?

Checking the documentation shows that its returns false on an error. So use the return status rather than or die() . It will return false if it fails, which you can log (or whatever you want to do) and then continue. Show activity on this post.


1 Answers

Does it have to die

Quite contrary, it shouldn't or die() ever.
PHP is a language of bad heredity. Very bad heredity. And or 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 use die() 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 any or 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.

like image 197
Your Common Sense Avatar answered Sep 21 '22 19:09

Your Common Sense