Skip to content

Instantly share code, notes, and snippets.

@marcaddeo
Created January 23, 2012 21:44
Show Gist options
  • Save marcaddeo/1665691 to your computer and use it in GitHub Desktop.
Save marcaddeo/1665691 to your computer and use it in GitHub Desktop.
A wrapper for PHP's MySqli class.
<?php
/*
** MySQL Abstraction Class
** Allows object creation for DB interaction on more advanced level.
** Objective of this class: Bring exceptions into MySQL errors to provide
** Better error verification and diagnosis.
**
** REQUIREMENTS:
** PHP5 with MySQLi support compiled and configured
** MySQL 4.1 for MySQLi interaction.
*/
/*
** Function to provide more success in variables passed to Class.
** Properly escapes all strings going to the MySQLi functions.
*/
function add_single_quotes($arg)
{
/* Single quote and escape single quotes and backslashes */
return "'" . addcslashes($arg, "'\\") . "'";
}
/*
** Function to provide more success in variables passed to class.
** Properly looks at the scalar type of var.
** Returns char type for mysqli_prepare.
**
*/
function getVarType() {
$type = "";
$args = func_get_args();
for ($x = 0; $x < count($args); $x++) {
// If integer, return i
if (is_int($args[$x])) {
$type .= "i";
}
// If string, return s
elseif (is_string($args[$x])) {
$type .= "s";
}
// If numerical double, return d
elseif (is_double($args[$x])){
$type .= "d";
}
// Mysqli->prepare will not allow anything but the above three, except for blob. For blob, manually set.
else {
die ("Invalid non-scalar value");
}
}
return $type;
}
/*
** Main Class. Child to main MySQLi class from PHP.
** Usage:
** $var = new DB('hostname', 'username', 'password', 'db');
** All functions are the same as original MySQLi class.
**
** Reminder: To make use of the exceptions, you must connect/query in a TRY block
** and CATCH the type of error.
**
** Connection Example:
**
**
** $test = new DB('hostname','username','password','dbname');
**
** Query Example:
**
** $query = "select * from users";
**
** Data Example:
**
** $result = $test->query($query);
**
** print_r($result->fetch_assoc());
**
**
*/
class DB extends mysqli {
function __construct() {
/* Try to connect, throw exception on error */
try {
/* Pass all arguments sent to constructor to the parent constructor */
$args = func_get_args();
eval("parent::__construct(" . join(',',array_map('add_single_quotes', $args)) . ");");
/* Throw an error if the connection fails */
if(mysqli_connect_error()) {
throw new ConnException(mysqli_connect_error(),mysqli_connect_errno());
}
}
/*Catch a connection error.*/
catch (ConnException $exception) {
printf("Connection Error Occurred.\n");
var_dump($exception->getMessage());
}
/*Catch any other error*/
catch (Exception $exception) {
printf("Other Error Occurred.\n");
var_dump($exception->getMessage());
}
}
function query($query) {
/*Attempt the query. Throw exception on error or failure.*/
try {
/*Perform a SQL Query utilizing the parent Query function*/
$result = parent::query($query);
/*Throw an error if the query fails.*/
if(mysqli_error($this)) {
throw new QueryException(mysqli_error($this),mysqli_errno($this));
}
}
/*Catch any query exception*/
catch (QueryException $exception) {
printf("Query Error Occurred.\n");
var_dump($exception->getMessage());
}
/*Catch any other exception*/
catch (Exception $exception) {
printf("Other Error Occurred.\n");
var_dump($exception->getMessage());
}
return $result;
}
function prepare($query) {
$this->stmt = new DBStmt($this, $query);
return $this->stmt;
}
}
?>
@sbrbot
Copy link

sbrbot commented Jul 29, 2016

What's the point of this MySQLi wrapper? MySQLi already can throw excceptions for errors, you just have to turn this on with mysql_report(MSQL_REPORT_ALL).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment