2017 © Pedro Peláez
 

library mysql-compat

Backward compatibility for old mysql_* functions with PDO

image

mattbit/mysql-compat

Backward compatibility for old mysql_* functions with PDO

  • Friday, June 22, 2018
  • by mattbit
  • Repository
  • 3 Watchers
  • 9 Stars
  • 2,525 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 4 Forks
  • 0 Open issues
  • 5 Versions
  • 24 % Grown

The README.md

Old mysql functions compatibility for PHP5.6 and PHP7

Build Status Scrutinizer Code Quality SensioLabsInsight, (*1)

This library tries to provide backward compatibility with the deprecated mysql_* functions., (*2)

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., (*3)

Although library provides an hackish replacement for mysql_real_escape_string, you ought to refactor your code to use prepared statements., (*4)

Requirements

PHP >= 5.6 with the PDO driver is required (PHP 7 is supported)., (*5)

Installation

You can install mysql-compat via composer:, (*6)

composer require mattbit/mysql-compat

Usage

The mysql_-equivalent functions are available through the facade class Mattbit\MysqlCompat\Mysql., (*7)

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., (*8)

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:, (*9)

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., (*10)

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):, (*11)

$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

The Versions

22/06 2018

dev-master

9999999-dev https://github.com/mattbit/mysql-compat

Backward compatibility for old mysql_* functions with PDO

  Sources   Download

MIT

The Requires

  • php >=5.6
  • ext-pdo *

 

The Development Requires

by Avatar mattbit

mysql php7 mysql functions

22/06 2018

v1.0.3

1.0.3.0 https://github.com/mattbit/mysql-compat

Backward compatibility for old mysql_* functions with PDO

  Sources   Download

MIT

The Requires

  • php >=5.6
  • ext-pdo *

 

The Development Requires

by Avatar mattbit

mysql php7 mysql functions

26/04 2018

v1.0.2

1.0.2.0 https://github.com/mattbit/mysql-compat

Backward compatibility for old mysql_* functions with PDO

  Sources   Download

MIT

The Requires

  • php >=5.4

 

The Development Requires

by Avatar mattbit

mysql php7 mysql functions

14/08 2016

v1.0.1

1.0.1.0 https://github.com/mattbit/mysql-compat

Backward compatibility for old mysql_* functions with PDO

  Sources   Download

MIT

The Requires

  • php >=5.4

 

The Development Requires

by Avatar mattbit

mysql php7 mysql functions

14/07 2016

v1.0.0

1.0.0.0 https://github.com/mattbit/mysql-compat

Deprecated mysql_* functions replacement with PDO

  Sources   Download

MIT

The Requires

  • php >=5.4

 

The Development Requires

by Avatar mattbit

mysql php7 mysql functions