Created
February 21, 2012 03:29
-
-
Save shanna/1873425 to your computer and use it in GitHub Desktop.
Tiny PHP MySQL abstraction.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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)) { | |
if (count($row) == 1) { | |
$values = array_values($row); | |
array_push($rows, $values[0]); | |
} | |
else { | |
array_push($rows, $row); | |
} | |
} | |
return $rows; | |
} | |
function first() { | |
$args = func_get_args(); | |
if ($result = call_user_func_array(array(&$this, 'query'), $args)) { | |
$row = mysql_fetch_assoc($result); | |
return $row && count($row) == 1 ? array_shift($row) : $row; | |
} | |
} | |
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); | |
} | |
function quote_value($value) { | |
return is_numeric($value) ? $value : "'" . $this->escape_value($value). "'"; | |
} | |
function escape_value($value) { | |
return is_numeric($value) ? $value : mysql_escape_string($value); | |
} | |
} | |
?> |
It was not given a choice :)
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
i can see two things wrong here