2

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!

Mister Woyng
  • 391
  • 2
  • 16
  • 2
    PDO basically is a wrapper (well, a database abstraction layer) - wouldn't it just be easier to refactor your code to use PDO rather than writing a wrapper for a wrapper? – CD001 Feb 03 '16 at 09:10
  • in php 5.6+, you "just" need to omit the deprecated_warning. the functions are removed in php7 - for which [search engine of your choice] provides dozens of tutorials how to restore the mysql_function-set. but in both cases, you should just plain migrate to mysqli_ or PDO and embrace the new functionality that just can't be used by simply using mysql_-named wrappers. – Franz Gleichmann Feb 03 '16 at 09:18
  • Also just replacing `mysql_*` calls with PDO calls is a huge missed chance to make your code sane by leveraging a sane API. – PeeHaa Feb 03 '16 at 09:24
  • @FranzGleichmann: Thank you for your comment. Yes, changing the error warning to error_reporting(E_ALL & ~E_DEPRECATED) is the easiest solution...getting rid of a problem by not looking at it :-) But then, it needs to be adressed in PHP7 again. – Mister Woyng Feb 04 '16 at 08:13
  • @FranzGleichmann: You said "...[search engine of your choice] provides dozens of tutorials how to restore the mysql_function-set." This is exactly what I am looking for, but I seem to be unable to pick the right search terms in the [engine of my choice]. Can you suggest some to me? – Mister Woyng Feb 04 '16 at 08:16
  • "php7 restore mysql functions"? – Franz Gleichmann Feb 04 '16 at 08:27
  • @FranzGleichmann: Thank you very much for the suggestion. It gives me only one useful result, which is to compile PHP7 mith mysql_*. Unfortunately, we cannot compile MySQL with our provider... – Mister Woyng Feb 05 '16 at 12:25
  • @all: I just found this: http://news.php.net/php.internals/53799 It says "Add pdo_mysql examples within the ext/mysql docs that mimic the current examples, but occasionally introduce features like prepared statements". Where can I find these examples? – Mister Woyng Feb 05 '16 at 12:33

2 Answers2

4

I found a solution: Mysql using Mysqli

This is exactly what I was looking for.

Mister Woyng
  • 391
  • 2
  • 16
  • How did this work for you? The script seems good enough, but I'm not sure it's 100%. I have some customers with (very) old PHP scripts (developers have disappeard) and server is upgraded. Your experience is much appreciated. – msoft Aug 02 '16 at 08:19
  • I have not used this "Mysql using Mysqli" yet, because we have upgraded from PHP 5.2 or 5.3 to PHP 5.6 or 5.7, so the mysql extension is still there. What is the PHP version of your Server(s) after the upgrade? – Mister Woyng Aug 03 '16 at 11:06
  • That correct. It is only for PHP 7 upgrade. Current upgrade is for 5.6 but 7 will not take too long now so (as you) I wanted to be prepared ;-) Thanks for the response. – msoft Aug 03 '16 at 14:39
  • Same for me ... PHP 7 will come. But I feel quite safe with the Mysql using Mysqli Script. – Mister Woyng Aug 05 '16 at 06:50
  • Can I help you with upgrading to PHP 5.6? – Mister Woyng Aug 05 '16 at 06:51
  • No need, thanks. Server upgrade is done by TD. I can upgrade PHP scripts without problem, but those customers will not pay for the work. So the wrapper will be a quick solution. The scripts are not mine so I'm not responsible, but they want to keep a working system. Always the same :-D If I find issues in this wrapper I'll report them. – msoft Aug 05 '16 at 11:29
0

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.

Your Common Sense
  • 156,878
  • 40
  • 214
  • 345
  • I was not looking for an automatic conversion tool. Only for an easy way to make an old, complicated and perfectly running osCommerce Shop survive a PHP update. – Mister Woyng Feb 04 '16 at 08:08
  • Then just gag E_DEPRECATED error as it was suggested in the comments. – Your Common Sense Feb 04 '16 at 08:11
  • Thank you, yes, I have read this. But this will not survive the migration to PHP7 . so I'd better avoid that. – Mister Woyng Feb 05 '16 at 12:27
  • 1
    Well, you have to make your mind first then. In your place I would ask myself *why* would I migrate an old, complicated and perfectly running osCommerce Shop to PHP7 – Your Common Sense Feb 05 '16 at 12:44
  • To me, the answer to "why" is: Because after the removal of mysql_*-functions in PHP7, the shop will not work any more, so something must be done. – Mister Woyng Feb 07 '16 at 16:50