Skip to Content
Mastering PHP 7
book

Mastering PHP 7

by Branko Ajzele
June 2017
Intermediate to advanced
536 pages
9h 49m
English
Packt Publishing
Content preview from Mastering PHP 7

Error handling

Handling errors around mysqli is relatively easy as we can use a simple try...catch block, as follows:

<?phpmysqli_report(MYSQLI_REPORT_ALL);try {   $mysqli = new mysqli('127.0.0.1', 'root', 'mL08e!Tq', 'sakila');} catch (Throwable $t) {   exit($t->getMessage());}

Ideally, we would want to use mysqli_sql_exception for more targeted MySQL exceptions-only handling:

<?phpmysqli_report(MYSQLI_REPORT_ALL);try {  $mysqli = new mysqli('127.0.0.1', 'root', 'mL08e!Tq', 'sakila');} catch (mysqli_sql_exception $e) {  exit($e->getMessage());}

We can pass one of the following report levels to the mysqli_report() function:

  • MYSQLI_REPORT_INDEX: This reports if a bad index or no index at all was used in a query
  • MYSQLI_REPORT_ERROR: This reports ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Start your free trial

You might also like

Learning PHP 7

Learning PHP 7

Antonio L Zapata (GBP)
Upgrading to PHP 5

Upgrading to PHP 5

Adam Trachtenberg

Publisher Resources

ISBN: 9781785882814Supplemental Content