note 49862 added to ref.mysqli

From: Date: Thu, 10 Feb 2005 17:34:36 +0000
Subject: note 49862 added to ref.mysqli
Groups: php.notes 
Request: Send a blank email to php-notes+get-84804@lists.php.net to get a copy of this message
I wrote a wrapper class for mysqli which adds exception handling to the code. It's based on the second tutorial at zend.com It partially relies on another small library which enables translation from php-warnings to exceptions, so I'm going to include this code as well. -------safe_mysqli.php------------ <?php //define('SQL_DEBUG', 1); // uncomment to always dump sql statement in Exception message (for debugging purposes only) require_once('exceptions.php'); install_exception_errorhandler(); // Translates PHP Warnings and User-Errors into corresponding Exceptions class DBException extends Exception {}; // Base class for Database Exceptions class DBConnectException extends DBException {} // Thrown when a Database connection fails // Thrown when problems with the execution or preparation of an SQL Query occurs. class SQLException extends DBException { private $sql; private $sqlerrmsg; function __construct($msg, $code, $sql) { /* Pass all arguments passed to the constructor on to the parent's constructor */ $this->sqlerrmsg = $msg; if (defined('SQL_DEBUG')) { $msg .= " in '$sql'"; } parent::__construct($msg, $code); $this->sql = $sql; } function getSQL() { return $this->sql; } function getSQLError() { return $this->sqlerrmsg; } } // Helper function for constructor function add_single_quotes($arg) { return "'" . addcslashes($arg, "'\\") . "'"; } // Written by Kai Londenberg K.Londenberg (at) librics (dot) de // http://www.librics.de/ based on an example at zend.com // mysqli wrapper class with exception support class safe_mysqli extends mysqli { function __construct() { /* Pass all arguments passed to the constructor on to the parent's constructor */ $args = func_get_args(); try { eval("parent::__construct(" . join(',', array_map('add_single_quotes', $args)) . ");"); } catch (WarningException $we) { if(mysqli_connect_error()){ throw new DBConnectException(mysqli_connect_error(), mysqli_connect_errno()); /* Throw an error if the connection fails */ } } } function query($query) { $result = parent::query($query); if(mysqli_error($this)){ throw new SQLException(mysqli_error($this), mysqli_errno($this), $query); } return $result; } function prepare($query) { $result = parent::prepare($query); if(mysqli_error($this)){ throw new SQLException(mysqli_error($this), mysqli_errno($this), $query); } return $result; } } ?> ---- Manual Page -- http://www.php.net/manual/en/ref.mysqli.php Edit -- http://master.php.net/manage/user-notes.php?action=edit+49862 Delete: added to the manual -- http://master.php.net/manage/user-notes.php?action=delete+49862&report=yes&reason=added+to+the+manual Delete: bad code -- http://master.php.net/manage/user-notes.php?action=delete+49862&report=yes&reason=bad+code Delete: spam -- http://master.php.net/manage/user-notes.php?action=delete+49862&report=yes&reason=spam Delete: useless -- http://master.php.net/manage/user-notes.php?action=delete+49862&report=yes&reason=useless Delete: other reasons -- http://master.php.net/manage/user-notes.php?action=delete+49862&report=yes Reject -- http://master.php.net/manage/user-notes.php?action=reject+49862&report=yes Search -- http://master.php.net/manage/user-notes.php

« previous php.notes (#84804) next »