https://github.com/mattbit/mysql-compat
Backward compatibility for old mysql_* functions
https://github.com/mattbit/mysql-compat
Last synced: about 1 year ago
JSON representation
Backward compatibility for old mysql_* functions
- Host: GitHub
- URL: https://github.com/mattbit/mysql-compat
- Owner: mattbit
- License: mit
- Created: 2015-09-12T22:22:31.000Z (almost 11 years ago)
- Default Branch: master
- Last Pushed: 2019-07-01T12:51:14.000Z (about 7 years ago)
- Last Synced: 2025-04-25T17:46:46.559Z (about 1 year ago)
- Language: PHP
- Size: 52.7 KB
- Stars: 25
- Watchers: 4
- Forks: 6
- Open Issues: 0
-
Metadata Files:
- Readme: readme.md
- License: LICENSE
Awesome Lists containing this project
README
# Old mysql functions compatibility for PHP5.6 and PHP7
[](https://travis-ci.org/mattbit/mysql-compat)
[](https://scrutinizer-ci.com/g/mattbit/mysql-compat/?branch=master)
[](https://insight.sensiolabs.com/projects/d9fcd340-4f29-46ac-966a-9df364b87aae)
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](https://github.com/illuminate/database).
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](https://getcomposer.org/):
```
composer require mattbit/mysql-compat
```
## Usage
The `mysql_`-equivalent functions are available through the facade class `Mattbit\MysqlCompat\Mysql`.
```php
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:
```php
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.
```php
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):
```php
$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](https://github.com/mattbit/mysql-compat/pull/7#issuecomment-467030421) for information)
- [ ] `mysql_stat`
- [ ] `mysql_tablename`
- [ ] `mysql_thread_id`
- [ ] `mysql_unbuffered_query`