Tutorial: Solve MySQL extension is deprecated & will be removed in the future of PHP (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:

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

The way with MySQLi would be like this:

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

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

<?php
// 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_*:

<?php
error_reporting(E_ALL ^ E_DEPRECATED);


If you have further questions: Just left them below in the comments.


Happy coding :)


Source: PHP Internal Wiki


Published by on 10. December 2012