EZ

Eduzan

Learning Hub

Eduzan
Eduzan / PHP

MySQL Database

Computer Science / PHP tutorial chapter - Published 2025-12-16 - PHP

What is MySQL?

MySQL is an open-source relational database management system (RDBMS) widely used in conjunction with PHP. It is currently one of the most popular database systems and is developed, distributed, and maintained by Oracle Corporation.

  • Data within a MySQL database is organized into tables consisting of rows and columns.
  • MySQL operates as a server-based database system.
  • It is well-suited for both small and large-scale applications.
  • Known for its speed, reliability, and ease of use, MySQL uses standard SQL (Structured Query Language).
  • MySQL can run on various platforms.

Downloading MySQL Database

You can download MySQL for free from the official MySQL website.

How to Connect PHP to a MySQL Database?

In PHP version 5 and above, MySQL can be accessed using two primary methods:

  • MySQLi extension.
  • PDO (PHP Data Objects).

Difference Between MySQLi and PDO

  • PDO supports 12 different types of databases, while MySQLi is exclusively for MySQL databases.
  • Both PDO and MySQLi offer object-oriented interfaces, but MySQLi also provides a procedural interface.
  • If you need to switch your project from MySQL to another database system, PDO allows this with minimal changes to the connection string and a few queries. With MySQLi, you would have to rewrite the entire codebase, including all queries.

Ways to Work with MySQL in PHP

There are three primary methods to connect and interact with a MySQL database in PHP:

1. MySQLi (Object-Oriented)
2. MySQLi (Procedural)
3. PDO (PHP Data Objects)

Connecting to a MySQL Database using PHP

Below are the three different approaches to connect to a MySQL database from a PHP script.

1. MySQLi Object-Oriented Approach: You can use the object-oriented approach with MySQLi to create a connection to the MySQL database.

Syntax:

<?php
$servername = "localhost";
$username = "username";
$password = "password";

// Create connection
$conn = new mysqli($servername, $username, $password);

// Check connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
}
echo "Connected successfully";
?>

Output:

Connected successfully

2. MySQLi Procedural Approach: You can also establish a connection using MySQLi’s procedural interface.

Syntax:

<?php
$servername = "localhost";
$username = "username";
$password = "password";

// Create connection
$conn = mysqli_connect($servername, $username, $password);

// Check connection
if (!$conn) {
    die("Connection failed: " . mysqli_connect_error());
}
echo "Connected successfully";
?>

Output:

Connected successfully

3. PDO Approach: PDO offers a unified interface for connecting to a MySQL database and can be easily adapted for other database systems.

Syntax:

<?php
$servername = "localhost";
$username = "username";
$password = "password";

try {
    $conn = new PDO("mysql:host=$servername;dbname=myDB", $username, $password);
    // Set PDO error mode to exception
    $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    echo "Connected successfully";
}
catch(PDOException $e) {
    echo "Connection failed: " . $e->getMessage();
}
?>

Output:

Connected successfully
End of lesson.