Tutorial: Solve MySQL extension is deprecated & will be removed in the future of PHP

Solve MySQL extension is deprecated & will be removed in the future of PHP

After a long discussion inside the PHP developers team they decided to mark the old mysql_* functions deprecated in PHP 5.5. In this short tutorial you will get a instruction how to solve this error message.

Why was mysql deprecated in PHP 5.5?

Johannes Schlüter, one of the PHP developers listed missing points like these:

  • Stored Procedures
  • Prepared Statements
  • (SSL-)Encryption
  • Compression
  • Full charset support

How to solve the warnings?

Currently mostly many MySQL connections in PHP use this construct:

$link = mysql_connect('localhost', 'user', 'password');
mysql_select_db($link, 'dbname');

The way with MySQLi would be like this:

$link = mysqli_connect('localhost', 'user', 'password', 'dbname');

To run database queries is also simple and nearly identical with the old way:

// Old
mysql_query('CREATE TEMPORARY TABLE `table`', $link);
// New
mysqli_query($link, 'CREATE TEMPORARY TABLE `table`');

In case you have the mysql_result() function in use, you can find on this website how to create a mysqli_result() alternative for MySQLi.

Alternative solutions

Besides the shown possibility there's are also solutions like using MySQLi in object oriented style or even switch to PHPs PDO (PHP Data Objects) like shown in this PDO Tutorial for MySQL Developers.

Filthy and fastest solution

Suppress all deprecated warnings including them from mysql_*:

error_reporting(E_ALL ^ E_DEPRECATED);

If you have further questions: Just leave them below in the comments or check out the best PHP frameworks for your needs to worry less about such errors like you solved in this tutorial.

Happy coding :)

Source: PHP Internal Wiki