note 49863 added to ref.mysqli
| From: | K dot Londenberg at librics dot de | Date: | Thu, 10 Feb 2005 17:35:30 +0000 |
| Subject: | note 49863 added to ref.mysqli | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-84805@lists.php.net to get a copy of this message | ||
Second part of the mysqli wrapper code since it didn't all get into one note
------ exception.php -------------
<?php
class FatalException extends Exception {};
class ErrorException extends Exception {};
class WarningException extends Exception {};
function exceptionErrorHandler($errno, $errstr, $errfile, $errline) {
switch ($errno) {
case E_USER_ERROR:
throw new FatalException($errstr, $errno);
break;
case E_USER_WARNING:
case E_WARNING:
throw new WarningException($errstr, $errno);
break;
case E_USER_NOTICE:
//throw new WarningException($errstr, $errno);
break;
default:
echo " - Unknown error - <b>$errstr</b>($errno) <br>";
break;
}
//return true;
}
$old_error_handler = null;
function install_exception_errorhandler() {
global $old_error_handler;
$old_error_handler = set_error_handler(exceptionErrorHandler);
}
function uninstall_exception_errorhandler() {
global $old_error_handler;
set_error_handler($old_error_handler);
$old_error_handler = null;
}
// returns formatted info-dump of an exception
function exception_dump(&$e) {
$res = "";
$res .= "<br><h2>".get_class($e)."</h2>\n";
$res .= "<h3>{$e->getMessage()} ({$e->getCode()})</h3>\n\n";
$res .= "file: {$e->getFile()}<br/>\n";
$res .= "line: {$e->getLine()}<br/>\n";
$res .= "<PRE>";
$res .= $e->getTraceAsString();
$res .= "</PRE><br>";
return $res;
}
?>
-------------------------------------------
Example-Code:
------ example.php ------------------------
<?php
require_once('safe_mysqli.php');
try {
$mysql = new safe_mysqli('locashost', 'user', 'pass',
'test');
$stmt = $mysql->prepare("SELECT city, city_accented, population, latitude, longitude from
world_cities where city like ? order by population desc limit 5");
$stmt->bind_param('s', $a);
$stmt->bind_result($city, $city_accented, $population, $latitude, $longitude);
foreach ($anfangsbuchstaben as $a) {
$stmt->execute();
while ($stmt->fetch()) {
echo "<b>$city_accented</b>: pop $population - Lat. $latitude Long.
$longitude<br>";
}
$stmt->free_result();
}
} catch (SQLException $e) {
die("SQL Error : ".$e->getSQLError()." in
<hr><PRE>".$e->getSQL()."</PRE><hr>");
} catch (DBException $e) {
die("Database Error: ". $e->GetMessage());
} catch (Exception $e) {
die(exception_dump($e));
}
?>
-----------------------------------------
Hope this helps ...
----
Manual Page -- http://www.php.net/manual/en/ref.mysqli.php
Edit -- http://master.php.net/manage/user-notes.php?action=edit+49863
Delete: added to the manual -- http://master.php.net/manage/user-notes.php?action=delete+49863&report=yes&reason=added+to+the+manual
Delete: bad code -- http://master.php.net/manage/user-notes.php?action=delete+49863&report=yes&reason=bad+code
Delete: spam -- http://master.php.net/manage/user-notes.php?action=delete+49863&report=yes&reason=spam
Delete: useless -- http://master.php.net/manage/user-notes.php?action=delete+49863&report=yes&reason=useless
Delete: other reasons -- http://master.php.net/manage/user-notes.php?action=delete+49863&report=yes
Reject -- http://master.php.net/manage/user-notes.php?action=reject+49863&report=yes
Search -- http://master.php.net/manage/user-notes.php