How to Connect MySQL Database to PHP Using MySQLi and PDO? PHP has gained recognition for its use in web development. It is embedded in HTML and executed on the server, generating dynamic page content. As of 2025, PHP remains extremely widely used, powering approximately 74% of all websites that utilize a server-side language. Web applications widely use MySQL as one of their preferred relational database choices.
In fact, MySQL is consistently ranked #2, behind Oracle, in terms of popularity on DB-Engines. PHP and MySQL anchor numerous web stacks, commonly referred to as LAMP/WAMP/MAMP stacks. When part of these stacks, PHP scripts extract the necessary information from the database to form HTML pages that display user accounts and stored posts or products.
Even in 2025, PHP and MySQL remain relevant due to their extensive ecosystem and ongoing updates. Most major PHP frameworks and content management system (CMS) platforms, such as WordPress, Drupal, Laravel, and Magento, rely on MySQL/MariaDB for data storage. For example, WordPress documentation explicitly notes that it uses MySQL as its database engine.
The combination of PHP and MySQL is also bundled in development environments like XAMPP, making it easy for beginners to set up a local PHP and MySQL environment. (In fact, the official XAMPP download page describes it as “an easy-to-install Apache distribution containing MariaDB, PHP, and Perl”.) In short, PHP serves the web pages and scripts, while MySQL provides a robust, relational database backend – a pairing that still powers a huge portion of the web.

XAMPP is a free, cross-platform package that bundles Apache (web server), PHP, and MySQL/MariaDB together for easy local development. Users can obtain XAMPP through its official Apache Friends website platform. For example, the download page (shown below) allows you to select the installer for Windows, Linux, or macOS.
Once downloaded, follow these steps for each platform:
On Linux: Download XAMPP. Run .run installer for Linux. Open a terminal and navigate to the folder where you downloaded the file. Run chmod +x xampp-linux-*-installer.run to make it executable, then execute it with sudo ./xampp-linux-*-installer.run. Follow the text-based installer prompts and accept the defaults. By default, XAMPP is installed into /opt/lampp. After installation, start the servers by running:
sudo /opt/lampp/lampp start
After installing XAMPP, you can verify the setup by visiting http://localhost in your browser. You should see the XAMPP welcome page or dashboard, confirming that Apache (PHP) and MySQL are running correctly. Your PHP project files should be placed in the htdocs folder inside the XAMPP directory (for example, C:\xampp\htdocs on Windows, /Applications/XAMPP/xamppfiles/htdocs on Mac, or /opt/lampp/htdocs on Linux). You can then access your projects at http://localhost/your-project-folder. When not in use, stop Apache and MySQL from the control panel or by running sudo /opt/lampp/lampp stop (Linux) to conserve resources.
With XAMPP (or any LAMP/WAMP stack) installed, let’s connect a PHP script to a MySQL database. We’ll use both MySQLi (the “Improved” MySQL extension) and PDO (PHP Data Objects). Follow these steps:

First, you need a database and a table to connect to. You can use phpMyAdmin (included in XAMPP) or the MySQL command line. For example, open http://localhost/phpmyadmin, and create a new database called testdb. Then, within testdb, create a table named ‘users’ with fields such as ‘id’, ‘name’, and ’email’. Alternatively, run SQL commands via phpMyAdmin or the terminal. An example SQL script to set this up would be:
CREATE DATABASE testdb;
USE testdb;
CREATE TABLE users (
id INT AUTO_INCREMENT PRIMARY KEY,
name VARCHAR(100) NOT NULL,
email VARCHAR(100) NOT NULL UNIQUE
);
This creates a database named testdb and a users table, where the id auto-increments and each email address must be unique. (phpMyAdmin provides a GUI for these operations, but the above SQL will work in any MySQL client.)

Now write a PHP script to connect to the database using MySQLi. For example, create a file mysqli_connect.php in your project folder and add this code:
<?php
$servername = "localhost"; // usually 'localhost' for XAMPP
$username = "root"; // default XAMPP MySQL user
$password = ""; // default XAMPP MySQL has no root password​:contentReference[oaicite:14]{index=14}
$dbname = "testdb";
// Create a new MySQLi connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check the connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
echo "Connected successfully to MySQLi";
?>
In this script, we use new mysqli() to connect to MySQL on localhost with the root user and no password (the XAMPP defaults). If the connection fails, $conn->connect_error will be non-empty, and we output the error. On success, it echoes “Connected successfully to MySQLi.”

Now, try connecting with PDO, which is a database abstraction layer in PHP. Create another file pdo_connect.php and add:
<?php
$dsn = "mysql:host=localhost;dbname=testdb;charset=utf8mb4";
$username = "root";
$password = ""; // XAMPP default
try {
// Create a PDO instance (connect to the database)
$pdo = new PDO($dsn, $username, $password);
// Set error mode to exception for better error handling
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
echo "Connected successfully to PDO";
} catch (PDOException $e) {
// If there is an error in the connection, we catch it here
die("Connection failed: " . $e->getMessage());
}
?>
This code uses a DSN (mysql:host=localhost;dbname=testdb) to specify the connection. We then set PDO::ATTR_ERRMODE to PDO::ERRMODE_EXCEPTION so that PDO will throw exceptions on error. If the connection succeeds, it prints “Connected successfully to PDO”; otherwise, it catches the exception and displays the error message.

Upload these PHP files to your XAMPP htdocs directory (for example, C:\xampp\htdocs\mysqli_connect.php). Start Apache and MySQL from the XAMPP Control Panel, then open http://localhost/mysqli_connect.php in your browser. You should see the success message. Repeat for pdo_connect.php. If you see “Connected successfully,” the connection is working. If an error occurs, the script will display it (e.g., “Connection failed: Access denied…”). Common issues include:
By following these steps, you now have PHP scripts that successfully connect to your MySQL database using both MySQLi and PDO.
Beyond phpMyAdmin (which comes with XAMPP), there are many graphical tools to help manage MySQL databases. Below are three of the most popular as of 2025, along with their features, pros/cons, and use cases:



Each of these tools has its niche: use phpMyAdmin for quick browser-based access (especially on local servers), MySQL Workbench for official, feature-rich MySQL work, and DBeaver if you need a cross-database tool. When choosing, consider whether you need a web interface (such as phpMyAdmin), advanced modeling tools (like Workbench), or multi-database support (like DBeaver).
In this guide, we cover how to set up a local PHP and MySQL environment and connect them using both the MySQLi and PDO extensions. We started by installing XAMPP (Apache, MySQL, PHP) on Windows, macOS, or Linux, and ensured that Apache and MySQL are running. We then demonstrated sample PHP code, using MySQLi (new mysqli(…)) and PDO (new PDO(…)) to establish a database connection, checking for errors, and outputting a success message. We also highlighted good practices, such as handling errors with exceptions (in PDO) and using prepared statements to secure your queries.
Lastly, we looked at popular MySQL database management tools in 2025. Tools like phpMyAdmin, MySQL Workbench, and DBeaver each offer unique features, pros, and cons for different workflows. For beginners on local development, phpMyAdmin (included with XAMPP) is very convenient. As you advance, learning a full IDE like Workbench or a multi-platform client like DBeaver can greatly simplify database design and administration.
As next steps, try creating more tables and writing SQL queries in your PHP code. You can also explore PHP frameworks (like Laravel or CodeIgniter) that use these extensions under the hood. Always practice secure coding – use parameter binding in MySQLi or PDO prepared statements to prevent SQL injection. And remember to secure your database (set strong passwords for MySQL users, restrict network access, etc.) before deploying any PHP-MySQL application to a live server. With this foundation, you’ll be well on your way to building dynamic, data-driven PHP web applications.

Hassan Tahir wrote this article, drawing on his experience to clarify WordPress concepts and enhance developer understanding. Through his work, he aims to help both beginners and professionals refine their skills and tackle WordPress projects with greater confidence.