Connecting PHP with MySQL (mysqli)

Introduction

To make PHP work with databases, we must connect PHP to MySQL.
This connection allows PHP to insert, read, update, and delete data from a database.

In this lesson, you’ll learn how to connect PHP with MySQL using mysqli.

What Is mysqli?

mysqli stands for MySQL Improved.

It:

  • Works only with MySQL databases
  • Is faster and more secure than old mysql_* functions
  • Supports prepared statements
  • Is widely used in modern PHP applications

⚠️ Old mysql_* functions are deprecated and should never be used.

Requirements Before Connecting

Make sure you have:

  • A database created
  • Database name
  • Username
  • Password
  • Database host (usually localhost)

You can find these details in your hosting control panel.

Basic mysqli Connection Syntax

$conn = mysqli_connect("hostname", "username", "password", "database_name");

Example: Connecting PHP to MySQL

<?php
$servername = "localhost";
$username = "db_username";
$password = "db_password";
$database = "php_tutorial";

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

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

echo "Database connected successfully!";
?>

Explanation

  • mysqli_connect() → Creates database connection
  • $conn → Connection variable
  • mysqli_connect_error() → Shows error message
  • die() → Stops execution if connection fails

Recommended: Store Connection in a Separate File

Create a file named db.php:

<?php
$servername = "localhost";
$username = "db_username";
$password = "db_password";
$database = "php_tutorial";

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

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

Then include it:

<?php
include "db.php";
?>

This keeps your code clean and reusable.

Using mysqli Object-Oriented Style (Optional)

$conn = new mysqli("localhost", "db_username", "db_password", "php_tutorial");

if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
}

echo "Connected successfully";

Both styles work — beginners can start with procedural style.

Common Connection Errors and Fixes

  • Access denied for user
  • → Check username and password
  • Unknown database
  • → Make sure database name is correct
  • Connection refused
  • → MySQL server might not be running
  • Host not allowed
  • → Check hosting configuration

Always double-check credentials.

Best Practice: Store Connection in a Separate File

Instead of repeating connection code on every page, create a file like db.php:

<?php
$host = "localhost";
$user = "root";
$password = "";
$dbname = "test_db";$conn = mysqli_connect($host, $user, $password, $dbname);if (!$conn) {
die("Database connection failed: " . mysqli_connect_error());
}
?>

Then include it:

<?php
require_once "db.php";
?>

This makes your project:

  • easier to maintain
  • easier to update credentials
  • cleaner to manage

Security Tips

  • Never expose database credentials publicly
  • Store credentials securely
  • Keep connection file outside public folders if possible
  • Use prepared statements for queries (coming next)
  • Do not show raw error messages on live sites
  • Avoid using root user on live servers

Mini Task for Students

Ask learners to:

  • Create a database
  • Write a PHP file to connect to it
  • Display a success message
  • Try changing the database name and observe the error
  • Fix the error again

This helps them understand connection errors in real life.

Summary

  • mysqli is used to connect PHP with MySQL
  • Connection is required before database queries
  • Always check for connection errors
  • Store connection code in a separate file

In the next tutorial, we’ll learn about Insert Data into MySQL using PHP.

Related Tutorials

Leave a Reply

Your email address will not be published. Required fields are marked *