AdyBlaze
Latest Article

PHP MySQL Connection Explained with Examples (Beginner Guide 2026)

23 Jul 2026

WhatsApp Facebook Telegram

Introduction

A dynamic website needs a database to store and retrieve information. Whether you’re building a blog, login system, e-commerce store, or CRM, PHP commonly connects with a MySQL database.

PHP provides two popular ways to connect to MySQL:

In this tutorial, we’ll focus on MySQLi, which is beginner-friendly and widely used.


What is MySQL?

MySQL is an open-source Relational Database Management System (RDBMS) used to store structured data.

Examples of data stored in MySQL:

PHP communicates with MySQL to read, insert, update, and delete this data.


Requirements

Before connecting PHP to MySQL, make sure you have:

For local development, you can use:


Database Credentials

A connection requires four basic details:

Host

Username

Password

Database Name

Typical local values:

Host: localhost

Username: root

Password:

Database: my_database

Connecting Using MySQLi

<?php

$conn = new mysqli(
    "localhost",
    "root",
    "",
    "my_database"
);

if($conn->connect_error){

    die("Connection Failed: " . $conn->connect_error);

}

echo "Connected Successfully";

?>

Output

Connected Successfully

Understanding the Code

new mysqli(
    host,
    username,
    password,
    database
);

The constructor creates a connection to the MySQL server.

If the connection fails, connect_error contains the error message.


Storing Connection in a Separate File

Professional projects keep database connections in a separate file.

config.php

<?php

$conn = new mysqli(
    "localhost",
    "root",
    "",
    "my_database"
);

if($conn->connect_error){

    die("Connection Failed");

}

?>

Then include it wherever needed:

<?php

require_once "config.php";

?>

This avoids repeating the connection code.


Closing the Connection

After completing database operations:

<?php

$conn->close();

?>

Although PHP closes connections automatically at the end of the script, explicitly closing them is a good habit.


Common Connection Errors

Wrong Username

Access denied for user

Wrong Password

Access denied

Database Not Found

Unknown database

MySQL Server Not Running

Can't connect to MySQL server

Always verify your credentials and ensure the database server is running.


Real-World Example

A login system typically follows this flow:

  1. Connect to the database.
  2. Receive email and password.
  3. Query the users table.
  4. Verify the password.
  5. Start a session if authentication succeeds.

Every database-driven PHP application begins with a successful database connection.


Best Practices


Frequently Asked Questions

Can PHP connect to MariaDB?

Yes. MariaDB is fully compatible with MySQLi in most cases.


Which is better: MySQLi or PDO?


Can one connection be reused?

Yes. Create the connection once and include the configuration file across your project.


Conclusion

Connecting PHP with MySQL is the first step toward building dynamic websites. By using MySQLi, separating connection logic into configuration files, and following best practices, you can create scalable and maintainable PHP applications.

Once the connection is established, you’re ready to perform CRUD operations such as inserting, updating, deleting, and retrieving data.


Next Tutorial

PHP INSERT Data into MySQL (2026 Guide)