Current Location: Home> Latest Articles> Comprehensive Guide to PHP Database Connection: From Basics to Advanced

Comprehensive Guide to PHP Database Connection: From Basics to Advanced

M66 2025-08-02

Comprehensive Guide to PHP Database Connection

In modern web development, databases are a vital component for building dynamic applications. PHP, as a widely used backend language, offers powerful functions for connecting and interacting with databases, especially MySQL. This article walks through the entire process, from basic setup to advanced usage.

Understanding SQL and Database Interaction

Before diving into code, it's essential to understand SQL (Structured Query Language). SQL is used to interact with relational databases, and common commands include SELECT, INSERT, UPDATE, and DELETE. PHP utilizes these statements to communicate with the database.

Establishing a Database Connection

The mysqli_connect() function is used to connect to a MySQL database. It takes four parameters: server name, username, password, and database name.

$servername = "localhost";
$username = "user";
$password = "password";
$dbname = "mydb";

$conn = mysqli_connect($servername, $username, $password, $dbname);

If the connection fails, you can use mysqli_connect_error() to retrieve the error message and debug the issue.

Executing SQL Queries

Once connected, SQL statements can be run using the mysqli_query() function.

// Execute a SELECT query
$sql = "SELECT * FROM users";
$result = mysqli_query($conn, $sql);

// Execute an INSERT query
$sql = "INSERT INTO users (name, email) VALUES ('John Doe', 'john@example.com')";
mysqli_query($conn, $sql);

Handling Query Results

For SELECT queries, the result set can be processed using mysqli_fetch_array() to retrieve data row by row.

while ($row = mysqli_fetch_array($result)) {
  echo $row['name'] . " " . $row['email'] . "\n";
}

Advanced PHP Database Techniques

After mastering the basics, PHP also supports advanced database operations:

  • Prepared Statements: Help prevent SQL injection and improve query performance.
  • Transactions: Use BEGIN, COMMIT, and ROLLBACK to ensure multiple operations are executed atomically.
  • Connection Pooling: Improves performance by reusing existing database connections (often implemented with frameworks or extensions).

Practical Example: Simple User Management System

Below is a basic example demonstrating how to build a user management system, from connecting to inserting and displaying users.

<?php

// Connect to the database
$servername = "localhost";
$username = "user";
$password = "password";
$dbname = "mydb";
$conn = mysqli_connect($servername, $username, $password, $dbname);

// Insert a new user
$name = "John Doe";
$email = "john@example.com";
$sql = "INSERT INTO users (name, email) VALUES ('$name', '$email')";
mysqli_query($conn, $sql);

// Fetch user list
$sql = "SELECT * FROM users";
$result = mysqli_query($conn, $sql);

echo "<ul>";
while ($row = mysqli_fetch_array($result)) {
  echo "<li>" . $row['name'] . " (" . $row['email'] . ")</li>";
}
echo "</ul>";

?>

Conclusion

PHP's database connection capabilities empower developers to build dynamic and data-driven applications. This guide has covered everything from basic setup to advanced techniques. With the practical example, you should now be confident in creating, querying, and managing databases using PHP.