Which one is faster and lighter - mysqli & PDO

2019-01-24 08:23发布

问题:

I am using MySQL with PHP5. I got to know that ancient mysql_* functions are no longer maintained and community has begun the deprecation process. So I decided to move away from mysql_*. The question is where to? I am looking for lighter, simpler and faster way. Somebody tell me which among these (mysqli and PDO) is;

  1. faster
  2. easy to learn and code
  3. consumes less space and memory

Note: Tutorial links in beginner level for these two will be helpful.

回答1:

PDO vs. MySQLi: Which Should You Use?

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. On the other hand, once you master PDO, you can use it with any database you desire!

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.

So if you’re still working with MySQLi, maybe it’s time for a change!

PDO vs. MySQLi: Which Should You Use? - By Dejan Marjanovic

Hope this helps .



回答2:

Along with the above question, here is a comparison chart;

And the information at Why you Should be using PHP’s PDO for Database Access is really helpful.



回答3:

About Performance

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. The MySQL extension was even faster.