How to Fix MySQLi Connection Errors and XAMPP Localhost Errors in Your PHP Project
You downloaded a PHP project, opened it on localhost, and instead of the app you got a red error about the database connection. This is the single most common thing that goes wrong when running a PHP and MySQL project on your own machine, and almost every cause has a quick fix. This guide walks through each error message, why it happens, and exactly what to change.
If MySQL will not even start in your XAMPP control panel, jump to MySQL will not start in XAMPP. If the app loads but throws a connection error, start with Access denied for user. Those two cover most cases.
- First, understand what the connection actually does
- Access denied for user ‘root’@’localhost’
- Connection refused or No such file or directory
- MySQL will not start in XAMPP (port conflict)
- Call to undefined function mysqli_connect()
- Unknown database ‘yourdbname’
- A clean connection file you can reuse
- Fast diagnostic checklist
- Frequently asked questions
First, understand what the connection actually does
Before fixing anything, it helps to know what a single line of PHP is trying to do. Most projects connect with something like this:
Those four values are, in order, the host (where the database server lives), the username, the password, and the database name. When you see a connection error, one of those four is wrong, or the MySQL server those values point to is not running. That is the whole picture. Every fix below is really just correcting one of those four things or starting the server.
If you only get a blank white page instead of an error, PHP is hiding the message. Add these two lines to the very top of your script so you can actually see what is failing:
Access denied for user ‘root’@’localhost’
This is the error you will hit most often:
It means MySQL is running fine, but the username and password your code is sending do not match what MySQL expects. In a default XAMPP install, the username is root and the password is empty. So the fix is usually making sure your connection line matches that:
// Default XAMPP: user is root, password is an empty string
$conn = mysqli_connect("localhost", "root", "", "your_database");
?>
Common reasons this still fails:
- Your code has a password like
"password"or"root"hardcoded, but XAMPP has no password set. Change it to an empty string"". - You set a MySQL root password earlier through phpMyAdmin and forgot. Either use that password in your code, or reset it in phpMyAdmin under User accounts.
- The project was written for a different setup (for example a live server where the user was not
root). Match the username to your local one.
The phrase using password: NO at the end of the error tells you PHP sent an empty password. If it says using password: YES, your code is sending some password that MySQL is rejecting. That one word tells you which side to look at.
Connection refused or No such file or directory
Two closely related errors:
These almost always mean the same thing: your code reached out to connect, but nothing was listening. Either the MySQL server is not running, or your host value is wrong. Work through it in this order:
1. Confirm MySQL is actually started
Open the XAMPP Control Panel and check that the MySQL row shows a green Running state. If it is not running, start it. If it refuses to start, that is a separate problem covered in the next section.
2. Try 127.0.0.1 instead of localhost
On some systems, especially macOS and certain Linux setups, the word localhost makes PHP look for a socket file that is not where it expects. Switching to the numeric address forces a normal TCP connection and skips the socket issue:
// If "localhost" gives No such file or directory, try this
$conn = mysqli_connect("127.0.0.1", "root", "", "your_database");
?>
This one change fixes a surprising number of “No such file or directory” errors on Mac and Linux.
MySQL will not start in XAMPP (port conflict)
If the MySQL row in XAMPP flashes and shuts down, or the log mentions that a port is in use, the usual cause is that something else on your computer is already using port 3306, the port MySQL wants. The most common culprit is a separate MySQL service installed elsewhere on the machine, or a previous XAMPP instance that did not close cleanly.
Two reliable ways to fix it:
Option A: stop the conflicting service
On Windows, open the Services panel, look for a service named MySQL or MySQL80, and stop it. Then start MySQL from XAMPP again. The conflict is gone because only one program is now claiming the port.
Option B: change the port XAMPP uses
If you cannot stop the other service, tell XAMPP to use a different port. In the XAMPP Control Panel, open the MySQL config and change the port from 3306 to 3307. Then your code has to point at that new port:
// Fifth argument is the port, only needed if you changed it
$conn = mysqli_connect("127.0.0.1", "root", "", "your_database", 3307);
?>
If you are deciding which local server to run in the first place, or you keep hitting port and service conflicts, our comparison of XAMPP vs WAMP vs Laragon for students explains which one tends to give beginners the fewest of these headaches.
Call to undefined function mysqli_connect()
This one is different from the others. It is not a database problem at all. It means PHP itself does not have the MySQLi extension switched on, so the function does not exist. In XAMPP this extension is usually on by default, but if you are on a custom PHP setup or edited your php.ini, it can be disabled.
Open your php.ini file, search for this line, and make sure it does not have a semicolon in front of it:
; Remove the leading semicolon if it has one, then save
extension=mysqli
Save the file and restart Apache from the XAMPP Control Panel so the change loads. The function will now exist.
Unknown database ‘yourdbname’
Good news: this error means your username, password, and server are all correct. PHP connected to MySQL successfully. The only problem is that the specific database named in your code does not exist yet. This happens constantly with downloaded projects, because the database has to be imported separately from the code.
To fix it, import the project’s SQL file:
- Open
http://localhost/phpmyadminin your browser. - Click New on the left and create a database with the exact name your code uses (matching capitalization).
- Select that database, go to the Import tab, choose the
.sqlfile that came with the project, and run it.
Most project downloads include a .sql file in a folder named database or db for exactly this step. Once it is imported, the connection will find the database and the app will load.
A clean connection file you can reuse
Here is a connection file that handles errors gracefully instead of dumping a raw warning. Save it as db.php and include it wherever you need the database. It tells you clearly what went wrong without exposing details to a visitor on a live site:
// db.php - reusable connection with clear error handling
$host = "127.0.0.1";
$user = "root";
$pass = "";
$name = "your_database";
$conn = @mysqli_connect($host, $user, $pass, $name);
if (!$conn) {
// During development this shows the real reason
die("Database connection failed: " . mysqli_connect_error());
}
?>
Using mysqli_connect_error() means that when something breaks, you get the actual reason (access denied, unknown database, and so on) printed in plain text, which makes every error above far faster to identify.
The die() with a detailed message is great while building, but remove the detailed text before putting a project on a real server. You do not want database error details visible to the public. Log them instead.
Fast diagnostic checklist
When a connection fails, run through these in order. One of them is almost always the cause:
- Is MySQL showing green and Running in the XAMPP Control Panel?
- Does your username say
rootand your password an empty string for a default XAMPP install? - Have you tried swapping
localhostfor127.0.0.1? - Did you import the project’s
.sqlfile into a database with the exact matching name? - Is anything else using port 3306, stopping MySQL from starting?
- Is
extension=mysqlienabled in yourphp.iniwith no leading semicolon?
If you would rather paste your exact error and get the matching fix instantly, try our PHP and XAMPP error fixer tool. It maps the common localhost, MySQL, and Apache error messages to their solutions.
Frequently asked questions
Why does my PHP project work on one computer but not another?
Almost always because the database credentials or the database itself differ between the two machines. The code is identical, but one computer has the right MySQL password and the imported database, and the other does not. Check the username, password, and that the .sql file was imported on the machine that fails.
Should I use mysqli or PDO for database connections?
Both work. MySQLi is slightly simpler and is what most beginner project downloads use, which is why this guide uses it. PDO is more flexible if you ever want to switch databases. For getting a downloaded project running, stick with whatever the project already uses rather than rewriting it.
Is “Access denied for user” a problem with my code or my database?
It is a credentials mismatch, so it sits between the two. Your code is sending a username and password that your MySQL server does not accept. Fix it by making the values in your connection line match your actual local MySQL setup, which for default XAMPP is user root with an empty password.
Do I need to start Apache too, or just MySQL?
For the database connection itself, only MySQL needs to be running. But to actually open your PHP pages in the browser through localhost, you also need Apache started. For a normal PHP and MySQL project, start both from the XAMPP Control Panel.
I changed my MySQL port to 3307, why does phpMyAdmin still work but my code does not?
phpMyAdmin reads its own configuration and follows the port change automatically. Your project code does not. You have to add the new port as the fifth argument in your mysqli_connect call, as shown in the port conflict section above, so your code points at the same place phpMyAdmin does.
Once your connection is solid, the rest of the project usually falls into place. If you want something clean to practice on, pick any project from the PHP source code library, get it connected with the steps above, and you have a working local app to build on.

