Skip to content

Instantly share code, notes, and snippets.

@shanna
Created November 17, 2009 04:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save shanna/236634 to your computer and use it in GitHub Desktop.
Save shanna/236634 to your computer and use it in GitHub Desktop.
<?php
# Mysql
#
# require_once('mysql.php');
# $db = new Mysql(dbname, server, user, pass);
# $sth = $db->query('select * from blah where name = ? limit 1', 'fred') || die($db->error());
# while ($row = mysql_fetch_assoc($sth))
# ...
#
class Mysql {
protected $db, $credentials, $resource;
function __construct() {
$this->db = @func_get_arg(0);
$this->credentials = array_slice(func_get_args(), 1);
}
function __destruct() { @mysql_close($this->resource); }
# TODO: Kinda sucky but if you don't use bind values from the start the simple regexp in bind() will barf on
# strings that contain escaped quotes like "foo\"bar".
function query() {
$args = func_get_args();
$statement = count($args) > 1 ? call_user_func_array(array(&$this, 'bind'), $args) : $args[0];
return mysql_query($statement, $this->resource());
}
function all() {
$rows = array();
$args = func_get_args();
if ($result = call_user_func_array(array(&$this, 'query'), $args))
while ($row = mysql_fetch_assoc($result)) array_push($rows, $row);
return $rows;
}
function first() {
$args = func_get_args();
if ($result = call_user_func_array(array(&$this, 'query'), $args)) {
return mysql_fetch_assoc($result);
}
}
function error() {
return $this->resource ? mysql_error($this->resource) : mysql_error();
}
function resource() {
if (@mysql_ping($this->resource)) return $this->resource;
@mysql_close($this->resource);
for ($wait = 1; $wait < 60; $wait *= 1.5) {
if ($this->resource = @call_user_func_array('mysql_connect', $this->credentials)) {
mysql_select_db($this->db, $this->resource);
return $this->resource;
}
error_log(sprintf('Mysql (re)connect: %s. Retrying in %.2fs.', mysql_error(), $wait));
sleep($wait);
}
die(sprintf('Mysql gave up trying to (re)connect: %s.', mysql_error()));
}
function bind() {
$statement = '';
$sql = func_get_arg(0);
$binds = array_slice(func_get_args(), 1);
$given = count($binds);
$replacements = 0;
$mismatch = false;
foreach (preg_split('/(\'[^\']*\'|"[^"]*"|`[^`]*`|\?)/', $sql, -1, 3) as $chunk) {
if ($chunk != '?') {
$statement .= $chunk;
continue;
}
$replacements += 1;
if (empty($binds)) $mismatch = true;
else $statement .= $this->quote(array_shift($binds));
}
if (!empty($binds) || $mismatch) die(sprintf('Binding mismatch: %d for %d in %s', $given, $replacements, $sql));
return $statement;
}
function quote($value) {
$values = is_array($value) ? $value : array($value);
$values = array_map(array(&$this, 'quote_value'), $values);
return is_array($value) ? join(', ', $values) : array_shift($values);
}
protected function quote_value($value) {
return "'" . mysql_real_escape_string($value) . "'";
}
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment