PHP

MySQLi vs PDO – which one to use? what is the best?

February 19, 2022

MySQLi vs PDO – which one to use? what is the best?

In this article you will learn which of the libraries to use to make connections and procedures with the database, the comparison of MySQLi vs PDO

MySQLi vs PDO cover

Hey you all programmers, how are you? Let’s learn more about PHP, MySQLi and PDO!

About MySQL

First, it is good to understand that the MySQLi approach ends up being more performant, as it uses fewer resources than the PDO, such as fewer database drivers

So this advantage must be taken into account if what you are looking for is performance

Also, its syntax is simpler and similar to its predecessor mysql_*

This can be a plus point if you are already used to the old library

Another plus point is object orientation, which is implemented in MySQLi

And also MySQLi performs all the functions that MySQL supports, which is not the case with PDO, there are some limitations

About the negative points we can mention the limitation of drivers, it only works for the MySQL database

It doesn’t have important features like named parameters or prepared statements

About the PDO

PDO works with 12 different drivers, including: MySQL, Oracle, PostegreSQL and SQLite

Which is cool because probably if you’re working on a relational DB project, it will be one of these

Uses Object Oriented, just like MySQLi

And it has prepared statemens, which is a way for the query not to be sent directly to the server, it goes through a preparation step before

Which is where it can be validated, for SQL Injection prevention, for example

Here’s an example of prepared statements:

<?php

 $sth = $dbh->prepare('SELECT *
  FROM customers
  WHERE name = ? AND age = ?');

 $sth->execute(array('Matheus', 29));
 $result = $sth->fetchAll();

?>

First, through the query, the attributes that we want to search in WHERE are received by “?”, so that there is prepare

Then the execution receives the actual parameters of the query

And about the negative points, we can say that it is slower than MySQLi, so the other one should be chosen when the problem is performance

There are some limitations in functionality that MySQL supports that are not implemented by PDO

Conclusion

In this article we learned the comparison of MySQLi vs PDO

The great reality is that MySQLi is an older resource and also more developed, in addition to having better performance.

PDO, on the other hand, gives you portability between banks and an advantage that is prepared statements

But there is room for both, given their pros and cons.

So the most correct thing is to analyze from application to application and not generalize or choose one of them out of fanaticism

Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x