5

According to the PHP.net web site, the MySQL extension is deprecated as of PHP 5.5+ Given the large number of PHP sites that use MySQL, I'd like to know what the best practices would be for existing sites using MySQL. Would you stabilize your site on a version of PHP that supported MySQL? Or take on the cost and time commitment to migrate to another DB extension?

Ray Paseur
  • 2,076
  • 2
  • 11
  • 17

5 Answers5

6

If you have the development time then I would highly recommend moving to PDO or MySQLi. Those extensions will be maintained and officially supported by the PHP group.

If your application is really stable and secure then I would disable the warning that 5.5 will generate. When the extension is removed, I am certain it will still exist as a PECL extension which I would then include.

Levi Morrison
  • 18,096
  • 5
  • 59
  • 79
4

The MySQL extension is being deprecated, not MySQL support as a whole. PHP is currently trying to push the new MySQLi extension as a replacement:

http://php.net/manual/en/book.mysqli.php

MySQLi improves upon the old functions, allowing you to perform MySQL functions procedurally (as before) or with objects.

Racktash
  • 121
  • 1
  • 5
  • Yeah, I know that part. PHP could have made MySQLI so much easier to implement if they had preserved the order of variables in the function calls! – Ray Paseur Dec 16 '12 at 16:31
4

I'd like to know what the best practices would be for existing sites using MySQL.

This one is easy to answer.
There should be no Mysql (as well as Mysqli or PDO) functions in your application code at all.
If you're using raw API functions like this

$data   = array();
$name   = mysql_real_escape_string($_GET['name']);
$result = mysql_query("SELECT * FROM table WHERE name = '$name'");
while ($row = mysql_fetch_row($result)){   
    $data[] = $row;
}

then you have to start rewriting your codes immediatey, using some database wrapper instead of direct raw API calls, to make it look like this:

$data = $db->getAll("SELECT * FROM table where name=?s",$_GET['name']); 

It will not only shorten and smarten your codes dramatically, but also put all the API calls in one library, where it can be changed to whatever new API when PHP team decided to kill another excellent ext.

Your Common Sense
  • 152,517
  • 33
  • 193
  • 313
2

As written on PHP manual, http://php.net/manual/en/intro.mysql.php

This extension is deprecated as of PHP 5.5.0, and is not recommended for writing new code as it will be removed in the future. Instead, either the mysqli or PDO_MySQL extension should be used. See also the MySQL API Overview for further help while choosing a MySQL API.

You should use mysqli.

dAm2K
  • 9,021
  • 4
  • 37
  • 43
1

If you going to move away from mysql, don't go for MySQLi but for PDO instead.

See mysqli or PDO - what are the pros and cons?

As I see it: PDO > MySQLi > mysql.

Some of PDO pro's:

  • Multidatabase
  • Object oriented (no global functions with weird side-effects if you don't provide the optional dblink)
  • Fast
  • Special MySQL specific options still available: http://php.net/manual/en/ref.pdo-mysql.php
Community
  • 1
  • 1
Bob Fanger
  • 24,735
  • 7
  • 52
  • 70