note 110971 modified in class.mysqli-sql-exception by danbrown
| From: | danbrown@php.net | Date: | Thu, 17 Jan 2013 15:27:40 +0000 |
| Subject: | note 110971 modified in class.mysqli-sql-exception by danbrown | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-193149@lists.php.net to get a copy of this message | ||
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();
}
?>
--was--
// Quick note on how to setup and use mysqli_sql_exceptions properly
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();
}
http://php.net/manual/en/class.mysqli-sql-exception.php