GithubHelp home page GithubHelp logo

mysql-compat's Introduction

Old mysql functions compatibility for PHP5.6 and PHP7

Build Status Scrutinizer Code Quality SensioLabsInsight

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

  • 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 for information)
  • mysql_​stat
  • mysql_​tablename
  • mysql_​thread_​id
  • mysql_​unbuffered_​query

mysql-compat's People

Contributors

arraintxo avatar mattbit avatar odimodugno avatar vecernik87 avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar

mysql-compat's Issues

real requirements?

README states that this library is requiring php >= 5.6 and PDO, but composer.json is requiring php >= 5.4 and no extension.

Get more out of query errors...

From mysql-compat/src/Connection.php:43

        $statement = $this->pdo->query($query);

        if ($statement === false) {
            throw new QueryException('Error executing the query.');
        }

For easier backwards compatability and troubleshooting, this might be more helpful if $this.getErrorInfo() were sent along with the exception.

As it is now, I don't seemingly have a way to get any error information myself, since I don't have access to the Connection from my calling code.

I ran into this because while select id, id from table works just fine, PDO apparently has an issue with the duplicate field.

I'm using this for now has a hack-fix:

throw new QueryException('Query error ' . serialize($this->getErrorInfo()));

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.