Thursday, August 27, 2015

How to solve mysql extension is deprecated and will be removed in the future: use mysqli or PDO instead?

This error or warning is occurred when we use the latest version of wamp or lamp servers. Normally we use the function mysql_connect() in order to establish the connection with the MySql Database server in PHP. We can call this function if the mysql extension is enabled by the PHP.

mysqli_extension how to use is deprecated in PHP PDO

         The problem with the latest version of PHP and wamp , they forces us to use latest and improved version of mysql database connectivity function that is mysqli (MySql Improved). It support many more features compared to mysqls default extension for database connectivity.

 Now let us examine the code that raises this warning.

<?php

     //Establish the connection to the database

$con = mysql_connect(‘localhost’,’root’,’password’);

 

//Select the database

mysql_select_db(‘dbname’);

 

//Query to be executed

$sql = “SELECT * FROM Table1”;

 

//Assigns the query results to a variable

$result = mysql_query($sql);

 

If(mysql_affected_rows()>0){

          echo “Query Executed”;

}

 

 ?>


Even though there are number of ways to solve this problem, we will be looking at a simple solution.It is given below


<?php

     $db = new mysqli('localhost', 'user', 'pass', 'demo');

 

if($db->connect_errno > 0){

    die('Unable to connect to database [' . $db->connect_error . ']');

}

$sql = “

    SELECT *

    FROM `users`

    WHERE `live` = 1

“;

if(!$result = $db->query($sql)){

    die('There was an error running the query [' . $db->error . ']');

}

while($row = $result->fetch_assoc()){

    echo $row['username'] . '<br />';

}

?>


   You can note that we established and selected the database in a single line by just creating an object of the MySqli class. It provides us various methods for querying the database. Also it provides a structures method for accessing the data from the database.

 If you are facing problems with the mysqli extension, you can enable or disable that in the following way.
steps-to-solve- mysql extension is deprecated
Steps to enable or disable the mysqli extension

if you still can't solve the problem please make your comment below.

5 comments:

  1. Thanks for this but I'm after a quick fix for my code if someone could do this for me?

    I replaced with mysqli_connect and added $sys_dbname but then failed again with the error check for the mysql_select_db which reported requiring a string given. Changed to mysqli_select_db and still the same output.

    Can someone quickly fix the code below for me?
    Thanks
    Dave

    function db_connect()
    {
    global $host,$user,$pwd,$errstr,$sys_dbname,$port;
    $strhost=$host;
    if($port && $port!=3306)
    $strhost=$strhost.":".$port;
    $conn = mysql_connect($strhost,$user,$pwd);
    if (!$conn || !mysql_select_db($sys_dbname,$conn))
    {
    trigger_error(mysql_error(), E_USER_ERROR);
    }
    return $conn;
    }

    function db_close($conn)
    {
    return mysql_close($conn);

    ReplyDelete
    Replies
    1. could you pleas show me the error message?

      Delete
    2. Thanks for this but I'm after a quick fix for my code if someone could do this for me?
      Warning||C:\wamp\www\jobfeature\management\connect.inc||7||mysql_select_db() expects at least 1 parameter, 0 given


      Delete
  2. I get "msg: mysql_connect(): The mysql extension is deprecated and will be removed in the future: use mysqli or PDO instead" message.
    Can someone, please, solve this peace of code for me?

    public function connect($host, $username, $password) {
    if ($this->connection = @mysql_connect($host, $username, $password)){
    if (isset($GLOBALS['utf8']) && $GLOBALS['utf8']) mysql_set_charset('utf8',$this->connection);
    // php4 version:
    //mysql_query("SET NAMES utf8");
    //mysql_query("SET CHARACTER SET utf8");
    //mysql_query("SET COLLATION_CONNECTION='utf8_general_ci'");
    return $this->connection;
    }
    else $this->_sql_error ('Error while connection to database');
    return false;
    }

    /**
    * close database
    *
    */
    public function close() {
    @ mysql_close($this->connection);
    }

    /**
    * select or reselect database
    *
    * @param string $database
    * @return boolean
    */
    public function db_select($database = '') {
    if (!empty ($database))
    $this->database = $database;
    if (mysql_select_db($this->database, $this->connection))
    return true;
    else
    $this->_sql_error('Error while connection to database');
    return false;

    ReplyDelete
  3. Thanks for this but I'm after a quick fix for my code if someone could do this for me?
    Warning||C:\wamp\www\jobfeature\management\connect.inc||7||mysql_select_db() expects at least 1 parameter, 0 given


    ReplyDelete