Mister Woyng
Mister Woyng

Reputation: 407

Migration to PHP 5.6: Writing a MySQL wrapper

I have to move a WebSite to PHP 5.6. Therefore, I need to change the way PHP connects to MySQL from the mysql_-like-type to either PDO or mysqli.

Someone proposed to write a wrapper (class), so that the old way of using

$db = mysql_connect("localhost", "testusr", "secretpass");
mysql_select_db("testdb", $db);

becomes

$db = new PDO("mysql:host=localhost;dbname=testdb", "testusr", "secretpass");

Apart from the connection, I have only basic SQL queries like INSERT, SELECT and DELETE.

Is there already such a thing that "translates" old mysql_ queries to mysqli or PDO?

It could maybe look like this:

function mysql_connect_wrapper ($host, $user, $pwd) {
$db = new PDO("mysql:host=localhost;dbname=testdb", "testusr", "secretpass");
return $db;
}

$db = mysql_connect_wrapper("localhost", "testusr", "secretpass");

Thanks a lot!

Upvotes: 2

Views: 2539

Answers (2)

Mister Woyng
Mister Woyng

Reputation: 407

I found a solution: Mysql using Mysqli

This is exactly what I was looking for.

Upvotes: 4

Your Common Sense
Your Common Sense

Reputation: 157871

There is no such tool for automatic conversion. Because not only API but also the way you run queries have to be changed: instead of adding variables in the query string directly, you have to substitute every variable with a placeholder, while variable itself have to be moved into execute(). As this is the main and the only important reason why you were deprived from familiar mysql_query().

Note that PDO is not the preferred way either. You may consider using an ORM for database interactions, and may be one that is part of a popular framework, like Laravel.

Upvotes: 0

Related Questions