Help with new PHP syntax

I got this error with the following code: Deprecated: mysql_connect(): The mysql extension is deprecated and will be removed in the future: use mysqli or PDO instead.
What should I change in the code to make it work without error? please. thanks.


$hostname = "localhost";
$username = "root";
$password = "";
$dbname = "";

 $conn = mysql_connect($hostname, $username, $password);
 if (!$conn)
 die('Could not connect: ' . mysql_error());
 mysql_select_db($dbname, $conn);


ok, thanks. I found the newer version.

$hostname = 'localhost';
$username = 'root';
$password = '';
$dbname = '';
$conn = count($tmp = explode(':', $hostname)) > 1 ? mysqli_connect($tmp[0], $username, $password, '', $tmp[1]) : mysqli_connect($hostname, $username, $password);
if (!$conn) {
    die('Could not connect: ' . mysqli_error($link));
mysqli_select_db($conn, $dbname);

What are you trying to do here exactly? Doesn’t seem to be correct.

It seems to allow for ports in the hostname, like localhost:3306, then split the hostname from the port number and supply them as separate parameters.

1 Like

It’s an old version of php code to connect mysql. The second version (new one) works without any errors. Actually I used a conversion site to get the code. here it is

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.