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.