PHPackages                             mattbit/mysql-compat - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Database &amp; ORM](/categories/database)
4. /
5. mattbit/mysql-compat

ActiveLibrary[Database &amp; ORM](/categories/database)

mattbit/mysql-compat
====================

Backward compatibility for old mysql\_\* functions with PDO

v1.1.1(6y ago)2653.7k↓32.8%6MITPHPPHP &gt;=5.6CI failing

Since Jul 14Pushed 6y ago4 watchersCompare

[ Source](https://github.com/mattbit/mysql-compat)[ Packagist](https://packagist.org/packages/mattbit/mysql-compat)[ Docs](https://github.com/mattbit/mysql-compat)[ RSS](/packages/mattbit-mysql-compat/feed)WikiDiscussions master Synced 1mo ago

READMEChangelog (6)Dependencies (2)Versions (7)Used By (0)

Old mysql functions compatibility for PHP5.6 and PHP7
=====================================================

[](#old-mysql-functions-compatibility-for-php56-and-php7)

[![Build Status](https://camo.githubusercontent.com/bd43d3072129c4043d745e0dfc9e3740a34893389dfe514f1b527260ce995639/68747470733a2f2f7472617669732d63692e6f72672f6d6174746269742f6d7973716c2d636f6d7061742e7376673f6272616e63683d6d6173746572)](https://travis-ci.org/mattbit/mysql-compat)[![Scrutinizer Code Quality](https://camo.githubusercontent.com/dec6cdb49011a0fa04fb58d97890d885ddd566d2e0c7fa6345c6c11b72ed1aa7/68747470733a2f2f7363727574696e697a65722d63692e636f6d2f672f6d6174746269742f6d7973716c2d636f6d7061742f6261646765732f7175616c6974792d73636f72652e706e673f623d6d6173746572)](https://scrutinizer-ci.com/g/mattbit/mysql-compat/?branch=master)[![SensioLabsInsight](https://camo.githubusercontent.com/6669e7ab6b122a4f542b0f9cb6db4c190e1971557ba1ab0f87938459a94025b9/68747470733a2f2f696e73696768742e73656e73696f6c6162732e636f6d2f70726f6a656374732f64396663643334302d346632392d343661632d393636612d3964663336346238376161652f6d696e692e706e67)](https://insight.sensiolabs.com/projects/d9fcd340-4f29-46ac-966a-9df364b87aae)

This library tries to provide backward compatibility with the deprecated `mysql_*` functions.

Caveat
------

[](#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
------------

[](#requirements)

`PHP >= 5.6` with the `PDO` driver is required (`PHP 7` is supported).

Installation
------------

[](#installation)

You can install `mysql-compat` via [composer](https://getcomposer.org/):

```
composer require mattbit/mysql-compat

```

Usage
-----

[](#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
-----

[](#to-do)

- `mysql_​affected_​rows`
- `mysql_​client_​encoding`
- `mysql_​close`
- `mysql_​connect`
- `mysql_​create_​db`
- mysql\_​data\_​seek (not supported)
- `mysql_​db_​name`
- `mysql_​db_​query`
- `mysql_​drop_​db`
- `mysql_​errno`
- `mysql_​error`
- `mysql_​escape_​string`
- `mysql_​fetch_​array`
- `mysql_​fetch_​assoc`
- `mysql_​fetch_​field`
- `mysql_​fetch_​lengths`
- `mysql_​fetch_​object`
- `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`
- `mysql_​insert_​id`
- `mysql_​list_​dbs`
- `mysql_​list_​fields`
- `mysql_​list_​processes`
- `mysql_​list_​tables`
- `mysql_​num_​fields`
- `mysql_​num_​rows`
- `mysql_​pconnect`
- `mysql_​ping`
- `mysql_​query`
- `mysql_​real_​escape_​string`
- `mysql_​result`
- `mysql_​select_​db`
- `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`

###  Health Score

38

—

LowBetter than 85% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity40

Moderate usage in the ecosystem

Community16

Small or concentrated contributor base

Maturity62

Established project with proven stability

 Bus Factor1

Top contributor holds 90% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~216 days

Recently: every ~262 days

Total

6

Last Release

2514d ago

PHP version history (2 changes)v1.0.0PHP &gt;=5.4

v1.0.3PHP &gt;=5.6

### Community

Maintainers

![](https://www.gravatar.com/avatar/26216f8764d0809ad74f49dc2766f527ad1019401d61ab22fb46a05cc54db553?d=identicon)[mattbit](/maintainers/mattbit)

---

Top Contributors

[![mattbit](https://avatars.githubusercontent.com/u/1101742?v=4)](https://github.com/mattbit "mattbit (45 commits)")[![arraintxo](https://avatars.githubusercontent.com/u/1630107?v=4)](https://github.com/arraintxo "arraintxo (2 commits)")[![odimodugno](https://avatars.githubusercontent.com/u/511536?v=4)](https://github.com/odimodugno "odimodugno (2 commits)")[![vecernik87](https://avatars.githubusercontent.com/u/25324519?v=4)](https://github.com/vecernik87 "vecernik87 (1 commits)")

---

Tags

mysqlPHP7mysql functions

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/mattbit-mysql-compat/health.svg)

```
[![Health](https://phpackages.com/badges/mattbit-mysql-compat/health.svg)](https://phpackages.com/packages/mattbit-mysql-compat)
```

###  Alternatives

[doctrine/dbal

Powerful PHP database abstraction layer (DBAL) with many features for database schema introspection and management.

9.7k578.4M5.6k](/packages/doctrine-dbal)[ifsnop/mysqldump-php

PHP version of mysqldump cli that comes with MySQL

1.3k5.5M69](/packages/ifsnop-mysqldump-php)[kirschbaum-development/eloquent-power-joins

The Laravel magic applied to joins.

1.6k25.2M34](/packages/kirschbaum-development-eloquent-power-joins)[scienta/doctrine-json-functions

A set of extensions to Doctrine that add support for json query functions.

58723.9M36](/packages/scienta-doctrine-json-functions)[clouddueling/mysqldump-php

PHP version of mysqldump cli that comes with MySQL

1.3k22.9k](/packages/clouddueling-mysqldump-php)[cytopia/mysqldump-secure

Secure mysqldump script with encryption, compression, logging, blacklisting and Nagios monitoring integration

1474.7k1](/packages/cytopia-mysqldump-secure)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
