Last active
December 10, 2015 11:58
-
-
Save hakre/4430914 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
# Supported Functions: | |
mysql_connect().................: Supported | |
mysql_pconnect()................: Supported | |
mysql_close()...................: Supported | |
mysql_select_db()...............: Supported | |
mysql_query()...................: Supported | |
mysql_unbuffered_query()........: n/a | |
mysql_db_query()................: n/a | |
mysql_list_dbs()................: n/a | |
mysql_list_tables().............: n/a | |
mysql_list_fields().............: n/a | |
mysql_list_processes()..........: n/a | |
mysql_error()...................: n/a | |
mysql_errno()...................: n/a | |
mysql_affected_rows()...........: n/a | |
mysql_insert_id()...............: n/a | |
mysql_result()..................: n/a | |
mysql_num_rows()................: Supported | |
mysql_num_fields()..............: n/a | |
mysql_fetch_row()...............: n/a | |
mysql_fetch_array().............: Supported | |
mysql_fetch_assoc().............: n/a | |
mysql_fetch_object()............: n/a | |
mysql_data_seek()...............: n/a | |
mysql_fetch_lengths()...........: n/a | |
mysql_fetch_field().............: n/a | |
mysql_field_seek()..............: n/a | |
mysql_free_result().............: n/a | |
mysql_field_name()..............: n/a | |
mysql_field_table().............: n/a | |
mysql_field_len()...............: n/a | |
mysql_field_type()..............: n/a | |
mysql_field_flags().............: n/a | |
mysql_escape_string()...........: n/a | |
mysql_real_escape_string()......: n/a | |
mysql_stat()....................: n/a | |
mysql_thread_id()...............: n/a | |
mysql_client_encoding().........: n/a | |
mysql_ping()....................: n/a | |
mysql_get_client_info().........: n/a | |
mysql_get_host_info()...........: n/a | |
mysql_get_proto_info()..........: n/a | |
mysql_get_server_info().........: n/a | |
mysql_info()....................: n/a | |
mysql_set_charset().............: n/a | |
mysql().........................: n/a | |
mysql_fieldname()...............: n/a | |
mysql_fieldtable()..............: n/a | |
mysql_fieldlen()................: n/a | |
mysql_fieldtype()...............: n/a | |
mysql_fieldflags()..............: n/a | |
mysql_selectdb()................: n/a | |
mysql_freeresult()..............: n/a | |
mysql_numfields()...............: n/a | |
mysql_numrows().................: n/a | |
mysql_listdbs().................: n/a | |
mysql_listtables()..............: n/a | |
mysql_listfields()..............: n/a | |
mysql_db_name().................: n/a | |
mysql_dbname()..................: n/a | |
mysql_tablename()...............: n/a | |
mysql_table_name()..............: n/a | |
Total: 7 of 61 (11%) supported. | |
resource(19) of type (mysql link) | |
bool(true) | |
resource(20) of type (mysql result) | |
int(2) | |
#0: array(4) { | |
[0] => | |
string(1) "1" | |
'id' => | |
string(1) "1" | |
[1] => | |
string(25) "value for option with ID1" | |
'option' => | |
string(25) "value for option with ID1" | |
} | |
#1: array(4) { | |
[0] => | |
string(1) "2" | |
'id' => | |
string(1) "2" | |
[1] => | |
string(25) "value for option with ID2" | |
'option' => | |
string(25) "value for option with ID2" | |
} | |
#2 (finally): bool(false) | |
bool(true) | |
Warning: mysql_close(): no MySQL-Link resource supplied in /pdo-mysql-compat.php on line 220 | |
Call Stack: | |
0.0006 150592 1. {main}() /pdo-mysql-compat.php:0 | |
0.0199 168960 2. mysql_close() /pdo-mysql-compat.php:220 | |
bool(false) | |
class PDO#6 (0) { | |
} | |
bool(true) | |
class PDOStatement#4 (1) { | |
public $queryString => | |
string(20) "SELECT * FROM config" | |
} | |
int(2) | |
#0: array(4) { | |
'id' => | |
string(1) "1" | |
[0] => | |
string(1) "1" | |
'option' => | |
string(25) "value for option with ID1" | |
[1] => | |
string(25) "value for option with ID1" | |
} | |
#1: array(4) { | |
'id' => | |
string(1) "2" | |
[0] => | |
string(1) "2" | |
'option' => | |
string(25) "value for option with ID2" | |
[1] => | |
string(25) "value for option with ID2" | |
} | |
#2 (finally): bool(false) | |
bool(true) | |
Notice: mysqlpdo::close(): no MySQL-Link resource supplied in /pdo-mysql-compat.php on line 88 | |
Call Stack: | |
0.0006 150592 1. {main}() /pdo-mysql-compat.php:0 | |
0.0338 170344 2. mysqlpdo_close() /pdo-mysql-compat.php:244 | |
0.0338 170376 3. mysqlpdo::close() data://text/plain,<?php function mysqlpdo_close($link_identifier = NULL) {return mysqlpdo::close($link_identifier);}:1 | |
0.0338 170472 4. trigger_error() /pdo-mysql-compat.php:88 | |
bool(false) |
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 | |
/** | |
* compat global static class to switch from mysql to PDO | |
* | |
* mysql_connect() -> mysqlpdo_connect() / mysqlpdo::connect() | |
* | |
* this does not solve any issues related to global static state, just switches the | |
* database driver to PDO. | |
* | |
*/ | |
class mysqlpdo | |
{ | |
static $links = array(); | |
/** | |
* connect to PDO as if using mysql_connect | |
* | |
* @param null $server | |
* @param null $username | |
* @param null $password | |
* @param bool $new_link | |
* @param int $client_flags | |
* @return PDO | |
*/ | |
public static function connect($server = null, $username = null, $password = null, $new_link = false, $client_flags = 0) { | |
$server === NULL && $server = ini_get("mysql.default_host"); | |
$username === NULL && $username = ini_get("mysql.default_user"); | |
$password === NULL && $password = ini_get("mysql.default_password"); | |
$link_key = "$server\0$username\0$password"; | |
if (!$new_link && isset(self::$links[$link_key])) { | |
return self::$links[$link_key]; | |
} | |
$pdo = new PDO('mysql:host=' . $server, $username, $password); | |
if (!isset(self::$links[$link_key])) { | |
self::$links[$link_key] = $pdo; | |
} | |
return $pdo; | |
} | |
/** | |
* @param string $server | |
* @param string $username | |
* @param string $password | |
* @param int $client_flags | |
* @return PDO | |
*/ | |
public static function pconnect($server = null, $username = null, $password = null, $client_flags = 0) { | |
$pdo = self::connect($server, $username, $password, false, $client_flags); | |
$pdo->mysqlpdo_persistent = true; | |
return $pdo; | |
} | |
/** | |
* @param PDO $link_identifier | |
* @return bool | |
*/ | |
public static function close(PDO $link_identifier = null) { | |
$link_key = null; | |
foreach (array_reverse(self::$links) as $key => $link) { | |
if ( | |
($link_identifier === null and !isset($link->mysqlpdo_persistent)) | |
or ($link === $link_identifier) | |
) { | |
$link_identifier = $link; | |
$link_key = $key; | |
break; | |
} | |
} | |
if ($link_key) { | |
unset(self::$links[$link_key]); | |
} | |
if ($link_identifier) { | |
unset($link_identifier); | |
return true; | |
} | |
trigger_error('mysqlpdo::close(): no MySQL-Link resource supplied'); | |
return false; | |
} | |
/** | |
* @param string $database_name | |
* @param PDO $link_identifier | |
* @return bool | |
*/ | |
public static function select_db($database_name, PDO $link_identifier = null) { | |
return (bool)self::query("USE $database_name", $link_identifier); | |
} | |
/** | |
* @param string $query | |
* @param PDO $link_identifier | |
* @return PDOStatement | |
*/ | |
public static function query($query, PDO $link_identifier = null) { | |
$link_identifier === NULL && $link_identifier = end(self::$links); | |
return $link_identifier->query($query); | |
} | |
public static function num_rows(PDOStatement $result) { | |
return $result->rowCount(); | |
} | |
public static function fetch_array(PDOStatement $result, $result_type = MYSQL_BOTH) { | |
$map = array( | |
MYSQL_BOTH => PDO::FETCH_BOTH, | |
MYSQL_ASSOC => PDO::FETCH_ASSOC, | |
MYSQL_NUM => PDO::FETCH_NUM, | |
); | |
isset($map[$result_type]) || $result_type = MYSQL_BOTH; | |
return $result->fetch($map[$result_type]); | |
} | |
public static function createfunctions($prefix = 'mysqlpdo', $proxy = __CLASS__) { | |
$blacklist = array_flip(array(__FUNCTION__, '', 'createfunc', 'debuginfo')); | |
foreach (get_class_methods($proxy) as $method) { | |
isset($blacklist[$method]) || self::createfunc($method, $prefix, $proxy); | |
} | |
} | |
public static function createfunc($name, $prefix = 'mysqlpdo', $proxy = __CLASS__) { | |
$refl = new ReflectionMethod($proxy, $name); | |
$definition = array(); | |
foreach ($refl->getParameters() as $index => $parameter) { | |
$definition[0][$index] = $definition[1][$index] = '$' . $parameter->getName(); | |
$definition[1][$index] .= $parameter->isOptional() | |
? ' = ' . var_export($parameter->getDefaultValue(), 1) | |
: ''; | |
} | |
$callvars = implode(', ', $definition[0]); | |
$signature = implode(', ', $definition[1]); | |
$function = $prefix . '_' . $name; | |
$code = "function $function($signature) {return $proxy::$name($callvars);}"; | |
if (ini_get('allow_url_include')) { | |
include('data://text/plain,<?php '.$code); | |
} else { | |
eval($code); | |
} | |
return; | |
} | |
public static function debuginfo() { | |
$functions = get_extension_funcs('mysql'); | |
$stats = array(0, 0); | |
echo "\n # Supported Functions:\n\n"; | |
foreach ($functions as $function) { | |
list(, $method) = explode('_', $function, 2) + array('', ''); | |
$supported = method_exists(__CLASS__, $method); | |
$supported && $stats[0]++; | |
$stats[1]++; | |
printf(" %'.-32s: %s\n", "$function()", $supported ? 'Supported' : 'n/a'); | |
} | |
$stats[2] = ($stats[0] / $stats[1]) * 100; | |
vprintf(" Total: %d of %d (%d%%) supported.\n", $stats); | |
} | |
} | |
mysqlpdo::debuginfo(); | |
echo "\n\n"; | |
// proxy into mysqldo_* global functions | |
mysqlpdo::createfunctions(); | |
// it won't qualify as legacy if you didn't configure your db | |
// in php.ini, right? | |
ini_set('mysql.default_host', 'localhost'); | |
ini_set('mysql.default_user', 'testuser'); | |
ini_set('mysql.default_password', 'test'); | |
$database_name = 'test'; | |
// legacy code using mysql_* | |
$link = mysql_connect(); | |
var_dump($link); | |
$selected = mysql_select_db($database_name); | |
var_dump($selected); | |
$result = mysql_query('SELECT * FROM config'); | |
var_dump($result); | |
$row_count = mysql_num_rows($result); | |
var_dump($row_count); | |
$counter = 0; | |
while ($row = mysql_fetch_array($result)) { | |
echo "#$counter: ", var_dump($row); | |
$counter++; | |
} | |
echo "#$counter (finally): ", var_dump($row); | |
$closed = mysql_close(); | |
var_dump($closed); | |
$closed = mysql_close(); | |
var_dump($closed); | |
echo "\n\n"; | |
// legacy code using PDO | |
$link = mysqlpdo_connect(); | |
var_dump($link); | |
$selected = mysqlpdo_select_db($database_name); | |
var_dump($selected); | |
$result = mysqlpdo_query('SELECT * FROM config'); | |
var_dump($result); | |
$row_count = mysqlpdo_num_rows($result); | |
var_dump($row_count); | |
$counter = 0; | |
while ($row = mysqlpdo_fetch_array($result)) { | |
echo "#$counter: ", var_dump($row); | |
$counter++; | |
} | |
echo "#$counter (finally): ", var_dump($row); | |
$closed = mysqlpdo_close(); | |
var_dump($closed); | |
$closed = mysqlpdo_close(); | |
var_dump($closed); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment