PHP (Core & Framework)

PDO vs. MySQLi: Which Should You Use?

pdo_vs_mysqli

When accessing a database in PHP, we have two choices: MySQLi and PDO. So what should you know before choosing one? The differences, database support, stability, and performance concerns will be outlined in this article.

You can use any of them as Both MySQLi and PDO have their advantages:

Both PDO and MySQLi offer an object-oriented API, but MySQLi also offers a procedural API – which makes it easier for newcomers to understand. If you are familiar with the native PHP MySQL driver, you will find migration to the procedural MySQLi interface much easier.

The core advantage of PDO over MySQLi is in its database driver support. At the time of this writing, PDO supports 12 different drivers, opposed to MySQLi, which supports MySQL only.

So, if your project’s requirement changes and you  have to use another database, PDO makes the process easy. You only have to change the connection string and a few queries. But in case of MySQLi, you will need to rewrite the entire code including queries.

Both support Prepared Statements. Prepared Statements protect from SQL injection, and are very important for web application security.

While both PDO and MySQLi are quite fast, MySQLi performs insignificantly faster in benchmarks – ~2.5% for non-prepared statements, and ~6.5% for prepared ones. Still, the native MySQL extension is even faster than both of these. So if you truly need to squeeze every last bit of performance, that is one thing you might consider.

Ultimately, PDO wins this battle with ease. With support for twelve different database drivers (eighteen different databases!) and named parameters, we can ignore the small performance loss, and get used to its API. From a security standpoint, both of them are safe as long as the developer uses them the way they are supposed to be used (read: prepared statements).

You can see more details on https://www.w3schools.com/