mysql-compat
mysql-compat copied to clipboard
Backward compatibility for old mysql_* functions
Old mysql functions compatibility for PHP5.6 and PHP7
This library tries to provide backward compatibility with the deprecated mysql_* functions.
Caveat
You really should not use this unless strictly needed: it's much better to refactor the existing code to use PDO and prepared statements directly or an ORM like Eloquent.
Although library provides an hackish replacement for mysql_real_escape_string, you ought to refactor your code to use prepared statements.
Requirements
PHP >= 5.6 with the PDO driver is required (PHP 7 is supported).
Installation
You can install mysql-compat via composer:
composer require mattbit/mysql-compat
Usage
The mysql_-equivalent functions are available through the facade class Mattbit\MysqlCompat\Mysql.
require __DIR__ . '/vendor/autoload.php';
use Mattbit\MysqlCompat\Mysql;
Mysql::connect('host', 'user', 'password');
Mysql::selectDb('my_db');
$result = Mysql::query('SELECT * FROM my_table');
$row = Mysql::fetchArray($result);
Note that the static methods are named in a camel-case like version of the original functions, e.g. mysql_fetch_array becomes Mysql::fetchArray.
If you are using PHP7 and want to re-define the old global functions and constants without touching existing code, you can use the Mysql::defineGlobals method:
require __DIR__ . '/vendor/autoload.php';
Mattbit\MysqlCompat\Mysql::defineGlobals();
mysql_connect('host', 'user', 'password');
mysql_select_db('my_db');
$result = mysql_query('SELECT * FROM my_table');
$row = mysql_fetch_array($result, MYSQL_BOTH);
If you need more control over the connections, the database manager allows you to access the underlying objects.
require __DIR__ . '/vendor/autoload.php';
use Mattbit\MysqlCompat\Mysql;
$manager = Mysql::getManager();
// Create a connection by specifying a custom DSN.
$connection = $manager->connect('mysql:dbname=mydatabase;host=myhost', 'user', 'pass');
// You can access the underlying PDO object
$pdo = $connection->getPdo();
// The rest of the code will use the last connection registered in the manager
$res = Mysql::query('SELECT * FROM my_table');
// But you can specify explicitly a connection as well
$res = Mysql::query('SELECT * FROM my_table', $connection);
This is particularly useful if you need to customize connection's DSN (e.g. to specify the charset):
$manager = Mysql::getManager();
$manager->connect('mysql:dbname=database;host=hostname;charset=customCharset', 'user', 'password');
// This will automatically use the connection above, with the right charset.
$res = Mysql::query('SELECT * FROM my_table');
To do
- [X]
mysql_âaffected_ârows - [ ]
mysql_âclient_âencoding - [X]
mysql_âclose - [X]
mysql_âconnect - [ ]
mysql_âcreate_âdb - [X] ~~mysql_âdata_âseek~~ (not supported)
- [ ]
mysql_âdb_âname - [ ]
mysql_âdb_âquery - [ ]
mysql_âdrop_âdb - [X]
mysql_âerrno - [X]
mysql_âerror - [X]
mysql_âescape_âstring - [X]
mysql_âfetch_âarray - [X]
mysql_âfetch_âassoc - [X]
mysql_âfetch_âfield - [X]
mysql_âfetch_âlengths - [X]
mysql_âfetch_âobject - [X]
mysql_âfetch_ârow - [ ]
mysql_âfield_âflags - [ ]
mysql_âfield_âlen - [ ]
mysql_âfield_âname - [ ]
mysql_âfield_âseek - [ ]
mysql_âfield_âtable - [ ]
mysql_âfield_âtype - [ ]
mysql_âfree_âresult - [ ]
mysql_âget_âclient_âinfo - [ ]
mysql_âget_âhost_âinfo - [ ]
mysql_âget_âproto_âinfo - [ ]
mysql_âget_âserver_âinfo - [ ]
mysql_âinfo - [X]
mysql_âinsert_âid - [ ]
mysql_âlist_âdbs - [ ]
mysql_âlist_âfields - [ ]
mysql_âlist_âprocesses - [ ]
mysql_âlist_âtables - [ ]
mysql_ânum_âfields - [X]
mysql_ânum_ârows - [ ]
mysql_âpconnect - [ ]
mysql_âping - [X]
mysql_âquery - [X]
mysql_âreal_âescape_âstring - [X]
mysql_âresult - [ ]
mysql_âselect_âdb - [X] ~~
mysql_âset_âcharset~~ (see issue #7 for information) - [ ]
mysql_âstat - [ ]
mysql_âtablename - [ ]
mysql_âthread_âid - [ ]
mysql_âunbuffered_âquery

