Quick note on how to setup and use mysqli_sql_exceptions properly
<?php
define("MYSQL_CONN_ERROR", "Unable to connect to database.");
// Ensure reporting is setup correctly
mysqli_report(MYSQLI_REPORT_STRICT);
// Connect function for database access
function connect($usr,$pw,$db,$host) {
try {
$mysqli = new mysqli($host,$usr,$pw,$db);
$connected = true;
} catch (mysqli_sql_exception $e) {
throw $e;
}
}
try {
connect('username','password','database','host');
echo 'Connected to database';
} catch (Exception $e) {
echo $e->errorMessage();
}
?>
The mysqli_sql_exception class
(PHP 5)
Introduction
The mysqli exception handling class.
Class synopsis
mysqli_sql_exception
extends
RuntimeException
{
/* Properties */
protected
$code
;
protected
$sqlstate
;
}Properties
- message
-
The error message.
- file
-
The file with the error.
- line
-
The line with the error.
- code
-
The code causing the error.
- sqlstate
-
The sql state with the error.
dronebraindeveloper at gmail dot com ¶
4 months ago
