PHP MySQL Connection – Connect PHP to MySQL Database

Introduction

One of the most popular databases used with PHP is MySQL. To work with database data in a PHP application, the first step is establishing a connection between PHP and the MySQL database.

A database connection allows a PHP script to communicate with the MySQL server. Once the connection is established, developers can perform various operations such as inserting data, retrieving records, updating information, and deleting entries from the database.

PHP provides multiple ways to connect with MySQL, including MySQLi (MySQL Improved) and PDO (PHP Data Objects). The MySQLi extension is commonly used in beginner tutorials because it is simple and easy to understand.

What is PHP MySQL Connection?

A PHP MySQL Connection is the process of connecting a PHP application to a MySQL database server so that the application can interact with the stored data.

When a PHP script connects to a MySQL database, it allows the script to perform various database operations such as:

  • Creating new records
  • Reading existing records
  • Updating data
  • Deleting data

To establish this connection, developers must provide certain database credentials, including:

  • Hostname – usually localhost
  • Username – the MySQL user account
  • Password – the database password
  • Database name – the specific database to access

PHP uses built-in functions to create this connection. If the connection is successful, the PHP script can execute SQL queries and manage data within the database.

Note: Without a database connection, a PHP application cannot interact with stored data.

Why It Is Used

A PHP MySQL connection is essential for building data-driven websites. Here are some reasons why it is widely used.

1. Store Website Data

Websites store large amounts of data such as user accounts, product details, blog posts, and comments in databases.

2. Retrieve Information

Applications can fetch and display stored information to users.

3. Update Existing Records

Developers can update user profiles, product prices, or other information stored in the database.

4. Build Dynamic Applications

Database connections allow websites to generate dynamic content instead of static pages.

5. Manage Large Data Efficiently

Databases make it easier to organize, search, and manage large amounts of information.

Because of these advantages, PHP and MySQL are widely used together in web development.

Syntax

The most common way to connect PHP to MySQL is by using the MySQLi extension.

Basic Syntax


$conn = mysqli_connect(hostname, username, password, database);

Example syntax:


$conn = mysqli_connect("localhost", "root", "", "mydatabase");
To check whether the connection was successful:
if (!$conn) {
  die("Connection failed: " . mysqli_connect_error());
}

If the connection is successful, the PHP script can start executing SQL queries.

Example

Below is a simple example of connecting PHP to a MySQL database.


$servername = "localhost";
$username = "root";
$password = "";
$database = "testdb";
$conn = mysqli_connect($servername, $username, $password, $database);
if (!$conn) {
  die("Connection failed: " . mysqli_connect_error());
}
echo "Connected successfully";

Explanation:

  1. The script defines the database credentials.
  2. The mysqli_connect() function attempts to connect to the MySQL server.
  3. If the connection fails, an error message is displayed.
  4. If the connection is successful, the script prints Connected successfully.

This confirms that PHP can communicate with the database.

Real-Life Example

A common real-life example of a PHP MySQL connection is a user login system.

When a user enters their username and password on a login page, the website must verify the credentials using data stored in a database.

The process works like this:

  1. The user enters login information in a form.
  2. The form sends the data to a PHP script.
  3. The PHP script connects to the MySQL database.
  4. The script checks if the username and password exist in the database.
  5. If the credentials match, the user is logged in successfully.

This process requires a reliable connection between PHP and the database. Without it, the application would not be able to verify user credentials or retrieve stored data.

Common Mistakes

Beginners often make several mistakes when creating a PHP MySQL connection.

1. Incorrect Database Credentials

Using the wrong username, password, or database name will cause the connection to fail.

2. Not Checking Connection Errors

Developers sometimes forget to check if the connection was successful.

Example solution:


if (!$conn) {
  die("Connection failed");
}

3. Hardcoding Sensitive Information

Database credentials should not be exposed publicly in code repositories.

4. Using Deprecated Functions

Older functions such as mysql_connect() are no longer supported. Developers should use MySQLi or PDO instead.

5. Not Closing the Connection

Although PHP usually closes connections automatically, it is good practice to close them manually.

Example:


mysqli_close($conn);

Conclusion

A PHP MySQL connection is the foundation of any database-driven web application. It allows PHP scripts to communicate with the MySQL server and perform operations such as inserting, retrieving, updating, and deleting data.

By using extensions like MySQLi, developers can easily establish a connection and execute SQL queries within their PHP applications. However, it is important to use correct database credentials and handle connection errors properly.

Related PHP Tutorials