PDO vs MySQLi in PHP – Comparison
In this lesson: PDO vs MySQLi compared — database support, prepared statements, named parameters, speed — with code. Choose the right one.
Two ways to talk to MySQL
PHP gives you two built-in ways to connect to a MySQL database: PDO and MySQLi. Both can do the same job — connect, run queries, use prepared statements. The choice affects how your code looks and how portable it is. Understanding the difference helps you pick the right one for your project.
Connecting with each
PDO connection:
<?php
$pdo = new PDO("mysql:host=localhost;dbname=school", "root", "");
?>
MySQLi connection:
<?php
$mysqli = new mysqli("localhost", "root", "", "school");
?>
Both open a connection to the same database; the syntax simply differs. PDO uses a single connection string; MySQLi passes the parts as separate arguments.
Prepared statements in both
PDO supports clear named placeholders:
<?php
$stmt = $pdo->prepare("SELECT * FROM users WHERE email = :email");
$stmt->execute([":email" => "aman@site.com"]);
?>
MySQLi uses ? and binds by position:
<?php
$stmt = $mysqli->prepare("SELECT * FROM users WHERE email = ?");
$stmt->bind_param("s", $email);
$stmt->execute();
?>
Both protect against SQL injection. PDO's named placeholders are often easier to read in longer queries.
Key differences
| Point | PDO | MySQLi |
|---|---|---|
| Databases | Many (MySQL, SQLite, etc.) | MySQL only |
| Named placeholders | Yes (:email) | No (? only) |
| Style | One consistent API | MySQL-specific |
| Prepared statements | Yes | Yes |
Which should you use
For most new projects, PDO is the recommended choice: it is database-independent, has a clean and consistent API, and supports named placeholders. Choose MySQLi only if you are certain the project will always use MySQL and you prefer its style. Either way, always use prepared statements.
Common mistakes
- Mixing PDO and MySQLi. Pick one and use it throughout — they are not interchangeable.
- Skipping prepared statements. Both APIs need them to stop SQL injection.
- Choosing MySQLi then needing another database. Switching later means rewriting the code.
- Forgetting error handling. Enable exceptions/errors so failures are visible during development.
Summary
- PDO and MySQLi are the two ways PHP connects to MySQL.
- PDO works with many databases and supports named placeholders.
- MySQLi works with MySQL only.
- Both support prepared statements — always use them.
- For new projects, PDO is usually the better default.
Next, connect and query safely in PHP MySQL connection, or build data operations with PHP CRUD.
Frequently Asked Questions
What is the main difference between PDO and MySQLi?
:email, which many developers find clearer.Which is better for a new project, PDO or MySQLi?
Do both support prepared statements?
What are named placeholders?
:email let you label each value in a query instead of using anonymous ? marks. PDO supports them; classic MySQLi uses ? only. Named ones make longer queries easier to read.