Issue with database connection after switching extensions
I’m having trouble with my PHP authentication system. Everything worked perfectly when I was using the old mysql extension, but now after converting to mysqli the connection fails completely.
The main error I keep getting is about hostname resolution, even though I didn’t change the database connection details. Here’s what I’m working with:
Original working version (mysql):
<?php
function connect_database() {
$host=":/path/to/mysql/socket/mysql.sock";
$user="dbuser";
$pass="dbpass";
$database="userdb";
$link = mysql_connect("$host", "$user", "$pass")
or die('Connection failed');
@ mysql_select_db("$database") or die('Database selection failed');
}
session_start();
if (isset($_POST['login'])) {
if (empty($_POST['user']) || empty($_POST['pass'])) {
echo "<script>alert('Please fill all fields'); window.location='login.html';</script>";
}
else {
$user=$_POST['user'];
$pass=$_POST['pass'];
connect_database();
$user = stripslashes($user);
$pass = stripslashes($pass);
$user = mysql_real_escape_string($user);
$pass = mysql_real_escape_string($pass);
$result = mysql_query("SELECT * FROM users WHERE user = '$user' AND pass = '$pass'")
or die(mysql_error());
$count = mysql_num_rows($result);
if ($count == 1) {
$_SESSION['current_user']=$user;
header("location: dashboard.html");
} else {
echo "<script>alert('Invalid credentials'); window.location='login.html';</script>";
}
mysql_close($link);
}
}
?>
Current broken version (mysqli):
<?php
function connect_database() {
$host=":/path/to/mysql/socket/mysql.sock";
$user="dbuser";
$pass="dbpass";
$database="userdb";
$connection = mysqli_connect("$host","$user","$pass","$database");
if (mysqli_connect_errno()) {
echo "MySQL connection failed: " . mysqli_connect_error();
}
}
session_start();
if (isset($_POST['login'])) {
if (empty($_POST['user']) || empty($_POST['pass'])) {
echo "<script>alert('Please fill all fields'); window.location='login.html';</script>";
}
else {
$user=$_POST['user'];
$pass=$_POST['pass'];
connect_database();
$user = stripslashes($user);
$pass = stripslashes($pass);
$user = mysqli_real_escape_string($connection, $user);
$pass = mysqli_real_escape_string($connection, $pass);
$result = mysqli_query($connection, "SELECT * FROM users WHERE user = '$user' AND pass = '$pass'")
or die(mysqli_error());
$count = mysqli_num_rows($result);
if ($count == 1) {
$_SESSION['current_user']=$user;
header("location: dashboard.html");
} else {
echo "<script>alert('Invalid credentials'); window.location='login.html';</script>";
}
mysqli_close($connection);
}
}
?>
The error message says it can’t resolve the hostname, but I’m using the same socket path that worked before. What am I missing in the conversion process?
The hostname resolution error happens because mysqli thinks your socket path is a network address, not a socket. I hit this same issue during a server migration last month. Your main problem is that connect_database() creates the connection locally but doesn’t share it with the rest of your script. You need to either return the connection or make it global. For socket connections, use localhost as the hostname and put the socket path as the sixth parameter: mysqli_connect('localhost', 'dbuser', 'dbpass', 'userdb', null, '/path/to/mysql/socket/mysql.sock'). Don’t forget to return the connection handle and assign it when calling the function.
Your problem is variable scope - connect_database() creates $connection locally but doesn’t return it. When you try using $connection in your main script, it’s undefined. I encountered the same issue last year when migrating legacy apps.
For socket connections, mysqli requires the socket path as the sixth parameter, not the hostname:
Make sure to call it with $connection = connect_database(); to capture the returning connection.
Additionally, consider that storing passwords in plaintext is a security risk; it’s a great opportunity to implement proper password hashing during this migration.
scope issue strikes again! your connect_database function isn’t global, so $connection doesn’t exist outside it. also, mysqli socket syntax is different - try mysqli_connect('localhost', 'dbuser', 'dbpass', 'userdb', null, '/path/to/mysql/socket/mysql.sock') and make sure you return the connection handle from your function.
Your $connection variable lives inside the connect_database() function, but you’re trying to use it outside. The function needs to actually return the connection.
For socket connections with mysqli, pass localhost as host and put the socket path in the 5th parameter:
Dealing with legacy PHP auth code is a nightmare though. I’ve automated similar migrations using Latenode workflows - they handle database ops, user auth, and API integrations without the PHP headaches.
Latenode connects directly to your database and handles auth logic through visual workflows. Way cleaner than debugging old PHP.
Your socket path syntax is wrong for mysqli. I hit this same issue during a migration project - mysqli handles socket connections totally differently than the old mysql extension. You’re passing the socket path as the hostname, but mysqli wants localhost as the host and the socket path as a separate parameter. Also, your function doesn’t return the connection object, so $connection is undefined in your main code. You need to return the connection handle and assign it to a variable. And you’re storing plaintext passwords in your database - that’s a huge security risk. Use password_hash() and password_verify() instead of direct string comparison.
Then call it with $connection = connect_database();
Maintaining PHP auth scripts like this gets messy fast though. I’ve switched to automating entire login systems with Latenode workflows instead of dealing with legacy PHP.
Latenode handles database connections, user auth, and session management through visual workflows. No more debugging scope issues or migration headaches. Plus it connects directly to MySQL without connection string hassles.
You can build the whole auth flow in minutes instead of wrestling with old PHP code.