PHP scripts running on cPanel hosting connect to MySQL using the hostname localhost. This is different from external database connections, which use the server’s IP address.
You will create a MySQL database and user in cPanel, then write a PHP script that connects to it using the MySQLi extension. This approach applies to any PHP application that reads from or writes to a database, including custom scripts and CMS installations.
Before you begin
- You need access to your cPanel account.
- PHP must be enabled on your hosting account. The MySQLi extension is included in all standard PHP installations.
- Your PHP file needs to be placed in your domain’s public directory (
public_htmlor a subdirectory).
Create a database and user in cPanel
Before writing your connection script, you need a database and a dedicated database user. Follow our guide on creating a MySQL database in cPanel to complete this step.
Once created, note down the following details – you will need them in the next section:
- Database name – formatted as
cpanelusername_databasename - Database user – formatted as
cpanelusername_username - Database password – the password you set for the database user
Write the PHP connection script
Create a PHP file in your hosting account and add the connection code below. Always use localhost as the hostname – this is required for PHP scripts connecting to MySQL within the same cPanel account.
Create a file called db_connect.php in your document root and add the following:
<?php
$hostname = 'localhost';
$database = 'cpanelusername_databasename'; // replace with your database name
$username = 'cpanelusername_dbuser'; // replace with your database user
$password = 'yourpassword'; // replace with your database password
$conn = mysqli_connect($hostname, $username, $password, $database);
if (!$conn) {
die('Connection failed: ' . mysqli_connect_error());
}
echo 'Connected successfully';
mysqli_close($conn);
?>
Replace the placeholder values with your actual database name, user and password from the previous section.
Test the connection
Upload or save the file to your hosting account and load it in a browser to confirm the connection works.
- Access the file in a browser.
Navigate toyourdomain.co.uk/db_connect.php, replacingyourdomain.co.ukwith your actual domain. - Check the output.
If the connection succeeds, the page displays Connected successfully. If it fails, an error message describes the problem. - Remove or protect the test file.
Deletedb_connect.phponce you have confirmed the connection. Never leave database credentials in a publicly accessible file.
Troubleshooting
Access denied for user
This error means the database user does not have permission to access the database, or the password is incorrect. Return to cPanel’s MySQL Databases section, confirm the user is assigned to the database with the correct privileges, and verify the password you copied matches what was set.
Unknown database error
This error appears when the database name in your script does not match an existing database. Check the exact name in cPanel under MySQL Databases – remember the cpanelusername_ prefix is part of the full name and must be included.
Call to undefined function mysqli_connect()
This means the MySQLi extension is not enabled for the PHP version your domain uses. See our guide on changing your PHP version in cPanel to confirm you are running a supported PHP version with MySQLi available.
Wrapping up
You created a MySQL database and user in cPanel and connected to it from a PHP script using MySQLi. Your PHP application can now read from and write to the database using this connection pattern.
For more advanced database access patterns, consider using PDO (PHP Data Objects), which supports multiple database types with the same connection interface. If your project requires connecting to a remote MySQL instance, see our guide on connecting to MySQL with an SSH tunnel.
Our cPanel hosting plans include MySQL databases and full PHP support as standard.