Do You Really Need or die with MySQLi?
It's common practice to use or die with MySQLi queries, as seen in the code below:
$update_result = mysqli_query( $link , $sql_update_login ) or die ('Unable to execute query. '. mysqli_error($link));
However, there are downsides to this approach:
Instead of manually checking for errors, consider configuring MySQLi to throw exceptions on error:
mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);
This way, any mysqli command can be written without the need for or die:
$result = mysqli_query($link, $sql);
In the event of an error, an exception will be thrown, providing detailed information about the issue.
For a more thorough approach to production-ready, uniform, and efficient error reporting in PHP, refer to the article on PHP error reporting.
The above is the detailed content of MySQLi's `or die`: Security Risk or Necessary Evil?. For more information, please follow other related articles on the PHP Chinese website!