What Is the Host Parameter in mysqli_connect()?
HNT-START
TITLE: What Is the Host Parameter in mysqli_connect()?
SLUG: mysqli-connect-host-parameter-explained
EXCERPT: Learn what the host parameter means in mysqli_connect(), when to use localhost or an IP address, and how to fix common MySQL connection errors.
CATEGORY: MySQL
You'll know exactly which host value to use in mysqli_connect() after a few minutes of checking your PHP and MySQL setup. The host parameter is the first argument of mysqli_connect(), and it tells PHP where to find the MySQL database server. If you're asking "what is the host parameter? what value should i put as the first argument in mysqli_connect()?", the answer depends on whether MySQL runs on your computer, another server, or inside a container.
Quick answer: Use localhost when PHP and MySQL run on the same machine. Use 127.0.0.1 when you specifically want to connect to local MySQL over TCP/IP. If your database runs on another server, enter its actual hostname or IP address. Don't use your website domain unless it also points to the MySQL server.
The confusion usually starts with a simple PHP example:
<?php
$conn = mysqli_connect(
'localhost',
'root',
'',
'my_database'
);
Someone copies this connection from a tutorial, changes the database name, and expects it to work everywhere.
It might work on XAMPP. It might fail immediately on shared hosting or inside Docker.
The reason isn't complicated. localhost describes where the database server is relative to PHP, not where your website is accessible from a browser.
We'll use a small order management application with a database called orders_db to explain how the connection works and how to fix it when the hostname is wrong.
How the host parameter works in mysqli_connect()
The syntax of mysqli_connect() is:
mysqli_connect(
$hostname,
$username,
$password,
$database,
$port,
$socket
);
Each parameter has a specific purpose.
| Parameter | Purpose | Example |
|---|---|---|
| Host | Location of the MySQL server | localhost |
| Username | MySQL account | app_user |
| Password | MySQL account password | Your actual password |
| Database | Database to select | orders_db |
| Port | TCP port number | 3306 |
| Socket | Local socket path, when needed | /var/run/mysqld/mysqld.sock |
The port and socket arguments are optional.
The first argument tells PHP where it should try to establish the database connection.
The second and third arguments are used to authenticate with MySQL. The fourth selects the database your application wants to work with.
Think of the connection process like this:
PHP application
|
| mysqli_connect()
|
v
MySQL host
(localhost or remote server)
|
v
MySQL authentication
(username + password)
|
v
Database: orders_db
|
v
Execute SQL queries
For our order management application, a normal connection might be:
<?php
$conn = mysqli_connect(
'127.0.0.1',
'app_user',
'your_password',
'orders_db',
3306
);
This assumes the database exists, the MySQL user has been created, and the user has permission to access it.
Here, PHP connects to the local machine over TCP/IP using port 3306.
It doesn't search for a database named 127.0.0.1. That value identifies the server, not the database.
A complete PHP MySQL connection example
Create a file called db.php:
<?php
mysqli_report(
MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT
);
$host = '127.0.0.1';
$username = 'app_user';
$password = 'your_actual_password';
$database = 'orders_db';
$port = 3306;
try {
$conn = mysqli_connect(
$host,
$username,
$password,
$database,
$port
);
mysqli_set_charset($conn, 'utf8mb4');
echo 'MySQL connection successful';
mysqli_close($conn);
} catch (mysqli_sql_exception $e) {
error_log(
'Database connection failed: ' .
$e->getMessage()
);
http_response_code(500);
exit('Unable to connect to the database.');
}
Replace the example username and password with your actual credentials.
If the database connection succeeds, you'll see:
MySQL connection successful
If the connection fails, the script logs the technical error rather than exposing sensitive information to website visitors.
That's the approach I'd use for diagnosing a connection in a real application.
For production, keep credentials in environment configuration outside the public web directory. Don't leave real database passwords inside publicly accessible diagnostic files.
Also, don't assume changing $host will fix every connection error. The hostname, authentication credentials, database name, and server configuration are separate things.
What is the host parameter? What value should I put as the first argument in mysqli_connect()?
The correct value depends on where your PHP application runs and where the MySQL server runs.
Here's the practical comparison:
| Your setup | Host value |
|---|---|
| PHP and MySQL on the same computer | localhost |
| Local MySQL connection over TCP/IP | 127.0.0.1 |
| Database on a separate server | Actual database hostname or IP |
| PHP and MySQL in separate Docker Compose services | MySQL service name |
| Shared hosting | Hostname supplied by hosting provider |
| Cloud-managed MySQL | Database endpoint supplied by provider |
The important part is choosing a value based on your actual environment.
Use localhost for a local PHP and MySQL installation
Suppose you're running Apache, PHP, and MySQL through XAMPP on Windows.
Both PHP and MySQL are installed on the same computer.
A connection can use:
$host = 'localhost';
Your code might look like:
<?php
$conn = mysqli_connect(
'localhost',
'app_user',
'your_password',
'orders_db'
);
In this setup, localhost is appropriate because the MySQL server is running locally.
You don't need to enter your computer's public IP address.
You also don't need to enter the URL you type into Chrome to open your PHP application.
One thing to check: XAMPP installations don't all have the same MySQL credentials.
Some fresh development installations may have a root account without a password. Others have been configured differently.
Don't assume the username is always root, and don't copy an empty root password into a production application.
A dedicated MySQL user is the safer choice.
Use 127.0.0.1 to connect through TCP/IP
You might wonder why PHP examples sometimes use:
$host = 'localhost';
while others use:
$host = '127.0.0.1';
Both refer to the local machine, but they don't always use the same connection method.
On Linux and other Unix-like systems, MySQL clients normally use a Unix socket when the hostname is localhost.
When you use 127.0.0.1, the client connects over TCP/IP.
For example:
<?php
$conn = mysqli_connect(
'127.0.0.1',
'app_user',
'your_password',
'orders_db',
3306
);
This explicitly requests a TCP connection to the local machine on port 3306.
That difference matters when PHP can't find the MySQL socket file.
For example, you might get:
mysqli_sql_exception: No such file or directory
If PHP is using a local Unix socket and the configured socket path is incorrect, switching to 127.0.0.1 may work.
However, MySQL must be listening on TCP/IP, and the account must have permission to connect.
Changing the host doesn't start a stopped MySQL server or fix an incorrect password.
One gotcha: on Unix-like systems, passing a TCP port alongside localhost doesn't necessarily force TCP. If you need TCP explicitly, use 127.0.0.1.
Use the actual database server address for remote MySQL
Now suppose your PHP application runs on one server and MySQL runs on another.
Your website might be available at:
https://app.example.com
But the MySQL server might use:
db.internal.example
Your connection should look like:
<?php
$conn = mysqli_connect(
'db.internal.example',
'app_user',
'your_password',
'orders_db',
3306
);
The hostname above is illustrative. Replace it with your actual database endpoint.
If the hosting provider gives you an IP address, you can use that instead.
For example:
$host = '192.0.2.10';
That address belongs to a reserved documentation range. It isn't a real database server for this example.
A remote MySQL server must also accept connections from your application.
This may involve MySQL account permissions, firewall settings, and secure network access.
Don't open MySQL to the entire public internet just to make a PHP connection work.
Where possible, use private networking and appropriate encryption for remote database connections.
Where you'll meet the host parameter in real PHP projects
The MySQL host setting appears in more places than the mysqli_connect() function.
You'll find it in local development tools, shared hosting, Docker, Laravel, and server configuration files.
XAMPP and WAMP
For XAMPP or WAMP, the most common starting point is:
$host = 'localhost';
If the connection doesn't work, open the XAMPP or WAMP control panel.
Check whether the database service is running.
Then confirm the configured port.
For example, if MySQL is using port 3307, your code must specify that port when connecting over TCP:
<?php
$conn = mysqli_connect(
'127.0.0.1',
'app_user',
'your_password',
'orders_db',
3307
);
A wrong port can produce a connection error even when your hostname is correct.
I'd check the service status and port before reinstalling MySQL or editing random configuration files.
Shared hosting and cPanel
On shared hosting, the database might run on the same server as PHP or on a separate database server.
Open your hosting control panel and find the MySQL connection details.
The host may be:
localhost
Or it may be a hostname provided by your hosting company.
Don't assume your website domain is also the database hostname.
Similarly, the address used to access phpMyAdmin is not necessarily the address that your PHP application should use for MySQL.
If your hosting company gives you a database hostname, use that value.
Docker Compose
Docker is one of the easiest places to get localhost wrong.
Imagine PHP running inside one container and MySQL running inside another.
From inside the PHP container, localhost refers to the PHP container itself.
It doesn't automatically refer to the MySQL container.
Consider this Compose configuration:
services:
app:
build: .
depends_on:
- mysql
mysql:
image: mysql:8.4
environment:
MYSQL_DATABASE: orders_db
MYSQL_USER: app_user
MYSQL_PASSWORD: change_this_password
MYSQL_ROOT_PASSWORD: change_this_root_password
This is an example configuration, assuming the application Dockerfile exists.
The example passwords must be replaced with suitable credentials or secret management before deployment.
Because the database service is named mysql, the PHP application running inside the app service can usually use:
$host = 'mysql';
Docker Compose provides name resolution between services sharing its network.
Using 127.0.0.1 inside the PHP container would point to the PHP container, not the MySQL container.
Also, depends_on doesn't guarantee that MySQL is ready to accept connections. Applications may need a database health check or connection retry.
If PHP runs directly on the Windows or Linux host rather than inside Compose, the connection setup is different. You may need a published database port.
Laravel DB_HOST configuration
Laravel uses the same underlying concept, although it normally connects through PDO instead of calling mysqli_connect().
In Laravel, you'll usually configure the host in .env:
DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=orders_db
DB_USERNAME=app_user
DB_PASSWORD=your_password
The DB_HOST value tells Laravel where MySQL is located.
If the database moves to another machine, the host value may need to change.
After updating Laravel environment configuration, cached settings may need to be cleared:
php artisan config:clear
If your deployment uses configuration caching, rebuild the cache according to your deployment process.
For a related database setup problem, read Why php artisan migrate:fresh --seed can't seed a MySQL table.
Common misunderstandings about MySQL connection errors
Developers sometimes change the hostname whenever a MySQL connection fails.
That can make troubleshooting harder because different errors point to different problems.
Read the actual exception first.
mysqli_connect(): Connection refused
A failed connection might report:
mysqli_sql_exception: Connection refused
On Windows, you may see:
No connection could be made because the target machine actively refused it
This usually means the requested network endpoint isn't accepting the connection.
Possible causes include a stopped MySQL service, an incorrect port, or a server listening on a different network interface.
Test the connection outside PHP.
If you have the MySQL command-line client installed, run:
mysql --protocol=TCP --host=127.0.0.1 --port=3306 --user=app_user --password orders_db
The client will prompt for the password.
If the command fails, investigate the MySQL service and network settings.
If the command succeeds but PHP fails, compare the exact host, port, and credentials.
On Windows, you can also test TCP connectivity through PowerShell:
Test-NetConnection 127.0.0.1 -Port 3306
Look for:
TcpTestSucceeded : True
This confirms that a TCP connection can be established to that endpoint.
It doesn't confirm MySQL authentication or database permissions.
Access denied for user 'root'@'localhost'
Another common error looks like:
Access denied for user 'root'@'localhost'
This means the MySQL server rejected the account during authentication or authorization.
It doesn't automatically mean the database hostname is wrong.
Check the username and password first.
MySQL accounts are identified by both username and client host information.
For example:
'app_user'@'localhost'
and:
'app_user'@'192.0.2.%'
can have different authentication settings and permissions.
The second address pattern is illustrative.
A detail that causes confusion is that the host part of a MySQL account describes where an account is permitted to connect from. It isn't necessarily the hostname PHP uses to find the database server.
Don't fix an access-denied error by granting unrestricted database access to every host.
Unknown database 'orders_db'
Suppose PHP reports:
Unknown database 'orders_db'
This indicates that the requested database wasn't found on the MySQL server that received the connection.
Check the database name.
If you're connecting to a remote server, also verify you're reaching the correct MySQL instance.
Changing:
$host = 'localhost';
to:
$host = '127.0.0.1';
won't create a missing database.
No such file or directory
On Unix-like systems, you may encounter:
mysqli_sql_exception: No such file or directory
A common cause is an incorrect local MySQL socket path.
For example, PHP might be trying to access a socket file that doesn't exist at the configured location.
Try connecting over TCP:
$host = '127.0.0.1';
If that works, investigate the socket configuration.
If TCP also fails, check the MySQL service and listening interfaces.
mysql_connect() is not mysqli_connect()
Some old PHP tutorials contain:
mysql_connect(
'localhost',
'root',
''
);
That function belonged to the old mysql extension, which was removed from PHP.
Modern PHP applications should use MySQLi or PDO.
If you're maintaining older code, see How to fix mysql_fetch_array() expects parameter 1 to be resource.
How to check which MySQL server PHP connected to
Sometimes your connection succeeds, but you're unsure whether PHP is using the intended database server.
For example, your development and production databases might have similar names.
You can ask MySQL which server and database you're using.
After establishing a working $conn connection, run:
<?php
$result = mysqli_query(
$conn,
"SELECT
DATABASE() AS database_name,
@@hostname AS server_hostname,
@@port AS server_port"
);
$row = mysqli_fetch_assoc($result);
echo '<pre>';
print_r($row);
echo '</pre>';
The query reports the currently selected database, the MySQL server's configured hostname, and its configured port.
Example output format:
Array
(
[database_name] => orders_db
[server_hostname] => database-server
[server_port] => 3306
)
The actual hostname and port depend on your environment.
This is useful when you suspect PHP is connecting to the wrong database instance.
Run the diagnostic only in a trusted environment. Remove or protect the output before deploying publicly.
You can find the formal argument definitions in the official PHP mysqli_connect() documentation.
When the MySQL host parameter matters and when it doesn't
The host parameter matters whenever PHP must establish a database connection.
It becomes particularly relevant when moving a website between servers, connecting to remote MySQL, configuring Docker networks, or changing the database service port.
It also matters when switching between a local Unix socket and a TCP/IP connection.
However, the host parameter doesn't control SQL syntax or the structure of your database.
Consider this query:
<?php
$result = mysqli_query(
$conn,
'SELECT unknown_column FROM orders'
);
If the unknown_column field doesn't exist, MySQL will report an SQL error.
Changing the host won't repair that query.
The same applies to missing tables, incorrect column names, data type mismatches, and invalid SQL statements.
A successful database connection proves only that PHP could reach the MySQL server, authenticate, and select the requested database.
It doesn't prove that every SQL query in the application will work.
I'd avoid changing connection settings once you've verified that the application reaches the correct server.
If a query is failing, inspect the query and the actual database schema instead.
FAQ
What is the default host value in mysqli_connect()?
PHP's MySQLi connection function can use a configured default hostname when the argument is omitted. In many development environments, this corresponds to localhost. It's better to specify the intended hostname explicitly so the connection doesn't depend on environment-specific defaults.
Should I use localhost or 127.0.0.1 for MySQL?
Use localhost for a normal local connection when your configuration supports it. Use 127.0.0.1 if you specifically need a local TCP/IP connection. On Linux, the distinction is particularly relevant because localhost normally selects a Unix socket.
Can I use an IP address as the first argument in mysqli_connect()?
Yes. The first argument accepts a hostname or IP address. 127.0.0.1 identifies the local loopback interface, while a reachable remote address can identify a database running on another machine.
What is the default MySQL port for mysqli_connect()?
MySQL commonly listens on TCP port 3306. Your server may use a different port, depending on its configuration. You can provide the required TCP port as the fifth argument of mysqli_connect().
Why does mysqli_connect() work on localhost but fail on hosting?
Your hosting server may have a different MySQL hostname, username, password, port, or access policy. Check the database connection details provided by your hosting company. Also verify that the PHP server can reach the database and that the MySQL account has permission to connect.
If you're still asking "what is the host parameter? what value should i put as the first argument in mysqli_connect()?", start by identifying where MySQL actually runs. Test that hostname and port with the MySQL command-line client, then use the verified connection details in your PHP application.
HNT-END


