Created
June 7, 2010 06:35
-
-
Save jakobo/428297 to your computer and use it in GitHub Desktop.
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 | |
if (class_exists('mysqli')) { | |
return; | |
} | |
class mysqli_result_facade { | |
protected $result; | |
protected $fields = array(); | |
public function __construct($result) { | |
$this->result = $result; | |
} | |
public function fetch_assoc() { | |
return mysql_fetch_assoc($this->result); | |
} | |
public function fetch_fields() { | |
if (count($this->fields)) { | |
return $this->fields; | |
} | |
$i = 0; | |
while ($i < mysql_num_fields($result)) { | |
$this->fields[]= mysql_fetch_field($result, $i); | |
} | |
return $this->fields; | |
} | |
public function free() { | |
mysql_free_result($this->result); | |
} | |
public function __get($name) { | |
if ($name == 'num_rows') { | |
return $this->num_rows(); | |
} | |
} | |
private function num_rows() { | |
return mysql_num_rows($this->result); | |
} | |
} | |
class mysqli { | |
protected $connect; | |
protected $results; | |
protected function freeAllResults() { | |
foreach ($this->results as $r) { | |
$r->free(); | |
} | |
$this->results = array(); | |
} | |
protected function doQuery($stmt, $preserve = false) { | |
if (!$preserve) { | |
$this->freeAllResults(); | |
} | |
error_log($stmt); | |
$result = mysql_query($stmt, $this->connect); | |
error_log(mysql_error($this->connect)); | |
if ($result !== TRUE && $result !== FALSE) { | |
$result = new mysqli_result_facade($result); | |
} | |
$this->results[] = $result; | |
} | |
public function __construct($host = "localhost", $user = "root", $pass = "", $port = 3306) { | |
$this->connect = mysql_connect($host, $user, $pass, $port); | |
$this->results = array(); | |
} | |
public function select_db($db) { | |
return mysql_select_db($db, $this->connect); | |
} | |
public function multi_query($statements) { | |
// transform escaped ; to token | |
$statements = explode(';', str_replace('\;', '__ESCAPED_SEMI__', $statements)); | |
foreach ($statements as $stmt) { | |
$this->doQuery(str_replace('__ESCAPED_SEMI__', '\;', $stmt), true); | |
} | |
return true; | |
} | |
public function query($statement) { | |
$this->doQuery($statement); | |
return (isset($this->results[0])) ? $this->results[0] : FALSE; | |
} | |
public function store_result() { | |
return $this->results[0]; | |
} | |
public function next_result() { | |
array_shift($this->results); | |
} | |
public function close() { | |
mysql_close($this->connect); | |
} | |
private function affected_rows() { | |
return mysql_affected_rows($this->connect); | |
} | |
private function warning_count() { | |
$result = $this->query('SHOW COUNT(*) WARNINGS'); | |
$row = $result->fetch_assoc(); | |
return $row['count']; | |
} | |
public function __get($name) { | |
if ($name == 'affected_rows') { | |
return $this->affected_rows(); | |
} | |
if ($name == 'warning_count') { | |
return $this->warning_count(); | |
} | |
} | |
} | |
function mysqli_connect_errno() { | |
} | |
function mysqli_connect_error() { | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment