Asked 30+ days ago
13 Dec 2016
Views 207
fatso

fatso posted

Deprecated: mysql_connect(): The mysql extension is deprecated in php

i am getting warning at my php website suddenly .
i am getting error with mysql_connect . it working previously.
Error or warning

Deprecated: mysql_connect(): The mysql extension is deprecated and will be removed in the future: use mysqli or PDO instead in  



so i do some effort . i checked with web-hosting people they said they upgrade php and want to solve it and so i did not get any problem.

so how can i remove deprecated mysql_connect function and use mysqli and PDO instead.
Mitul Dabhi

Mitul Dabhi
answered Jan 21 '18 20:16

you should use mysql class and PDO also instead

1. Mysql class


$conn= new mysqli('your-server-name','database-name','database-password','database-name');// not needed mysql_select_db function 
 $query= "select * form tablename";
if ($result = $mysqli->query($query)) {
    while ($row = $result->fetch_row()) {
        printf("%s (%s,%s)\n", $row[0], $row[1], $row[2]);
    }
    /* free result set */
    $result->close();
}

you can prepare statement before run it .
like

$query = "INSERT INTO tablename(`field-1`,`field-2`) VALUES (?,?)";
$stmt = $mysqli->prepare($query);
// now you can replace blank value in query with as many time as you want with bind 
$stmt = $stmt ->bind_param("ss","value-1",'value-2');
//first argument stand for value type here s for string value
/* execute prepared statement */
$stmt->execute();



yes Mysql extension deprecated soon in php 7 , should be replaced with Mysqli

so mysql_connect should be replace with mysqli_connect


$conn=mysqli_connect('your-server-name','database-name','database-password');
//instead of  $conn=mysql_connect('yourserver','database-name','database-password');
mysqli_select_db($conn,"database-name");// need to pass the connection reference as first argument
//instead of mysql_select_db("database-name");
mysqli_query($conn,"select * from tablename");
//instead of msyql_query("select * from tablename");


so in short use mysqli instead of mysql and pass the connection as first argument in the function .
but this function also removed in future version so avoid to use it also

Edit Answer
Share
jaman

jaman
answered Jan 21 '18 20:16

i got same error and i put simply @ before mysql_connect and it get disappeared


@mysql_connect('localhost',$dbusername,$dbpassword);


Edit Answer
Share
its not seem to be good solution .dont hide problem but solve it - Mitul Dabhi  
Dec 12 '16 09:37
Post Answer