Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Future of the PHP MySQL functions [closed]

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?

like image 401
Ray Paseur Avatar asked Dec 16 '12 15:12

Ray Paseur


5 Answers

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.

like image 164
Levi Morrison Avatar answered Nov 10 '22 00:11

Levi Morrison


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.

like image 28
Racktash Avatar answered Nov 09 '22 23:11

Racktash


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.

like image 4
Your Common Sense Avatar answered Nov 10 '22 01:11

Your Common Sense


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.

like image 2
dAm2K Avatar answered Nov 10 '22 00:11

dAm2K


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
like image 1
Bob Fanger Avatar answered Nov 10 '22 01:11

Bob Fanger