Steps to Establish a PHP Database Connection
Let’s go through the process of connecting PHP to a MySQL database step-by-step. We’ll cover the following topics:
- Creating a Database in MySQL
- Setting Up the PHP Code for Database Connection
- Testing the Database Connection
- Handling Errors in PHP Database Connection
Creating a Database in MySQL
Before connecting PHP to a MySQL database, you need to create the database. If you’re using a local server like XAMPP or WAMP, you can do this using PHPMyAdmin.
- Open PHPMyAdmin (usually found at http://localhost/phpmyadmin/).
- Click on Databases.
- Enter a name for your new database, e.g., my_database.
- Click Create.
Now you have a database to work with.
Setting Up the PHP Code for Database Connection
PHP uses the mysqli (MySQL Improved) extension or PDO (PHP Data Objects) to interact with MySQL databases. We’ll use mysqli in this example, as it's easier to use for beginners.
Here's the basic code to connect PHP to your MySQL database using mysqli:
<?php
// Database connection details
$servername = "localhost"; // The server where the database is hosted (usually localhost)
$username = "root"; // Your MySQL username (usually 'root' for local setups)
$password = ""; // Your MySQL password (empty for default setups)
$dbname = "my_database"; // The name of the database you want to connect to
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error); // Stop the script if there's an error
}
echo "Connected successfully"; // If connection is successful, display this message
// Close connection
$conn->close();
?>

You can also try this code with Online PHP Compiler
Run Code
Explanation:
- $servername: The name of the server where your database is hosted. For local setups, it's usually "localhost."
- $username: The username used to log in to the database. By default, it is “root” for most local setups.
- $password: The password for your MySQL user. If you're using a local server like XAMPP, this is often empty by default.
- $dbname: The name of the database you want to connect to.
- new mysqli(): This line creates a new connection to the MySQL server.
- $conn->connect_error: If there is a problem connecting to the database, this checks for errors and stops the script.
- $conn->close(): Closes the database connection once you're done.
Testing the Database Connection
After writing the code, you can test your connection.
- Save your file with the .php extension, e.g., connect.php.
- Place the file in your web server's root directory (e.g., C:/xampp/htdocs for XAMPP).
- Open a web browser and go to http://localhost/connect.php.
If the connection is successful, you should see the message:
Connected successfully
If there is an error, you’ll see an error message with details about what went wrong (e.g., incorrect credentials, wrong database name).
Handling Errors in PHP Database Connection
Sometimes things go wrong, and it’s important to handle those errors to troubleshoot effectively.
<?php
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error); // Display error and stop execution
} else {
echo "Connected successfully";
}
// Additional actions like querying the database can go here
// Close connection
$conn->close();
?>
The key part is if ($conn->connect_error). If there's an issue with the connection, this will display an error message that helps you identify what went wrong.
Frequently Asked Questions
How do I create a database in MySQL for PHP?
To create a database in MySQL, you can use PHPMyAdmin (if you are using a local server like XAMPP or WAMP). Go to the Databases tab, enter a name for your database, and click Create. Alternatively, you can use SQL commands to create a database directly in your PHP script.
What should I do if my PHP database connection fails?
First, check if your database credentials (username, password, and database name) are correct. If the server is running locally, make sure it's active. Also, verify that your server allows PHP to connect to the database, especially for remote connections.
Can I use PHP to connect to a database other than MySQL?
Yes, you can! While this article focuses on MySQL, PHP can connect to other databases like PostgreSQL, SQLite, and more using different extensions. For example, to connect to a PostgreSQL database, you would use the pg_connect() function instead of mysqli.
Conclusion
This article provided an easy-to-follow guide for connecting PHP to a MySQL database. You’ve learned how to set up a connection, test it, and handle errors that may arise during the process. With this knowledge, you can now start developing dynamic applications that interact with databases using PHP.
You can also check out our other blogs on Code360.