Choosing between MySql, MySqli and PDO
Which API should I choose to connect to a MySql Database?
Choosing between MySql API depends on what do you really need.
So which one should you really choose?
Below is a table that shows comparisons of each API.
|
MySqli |
PDO |
MySql |
PHP version introduced |
5.0 |
5.1 |
2.0 |
Included with PHP 5.x |
Yes |
Yes |
Yes |
Included with PHP 7.x |
Yes |
Yes |
No |
Development status |
Active |
Active |
Maintained in 5.x; Removed in 7.x |
Lifecycle |
Active |
Active |
Deprecated in 5.x; Removed in 7.x |
Recommended for new projects |
Yes |
Yes |
No |
OOP Interface |
Yes |
Yes |
No |
Procedural Interface |
Yes |
No |
Yes |
API supports non-blocking, asynchronous queries with mysqlnd |
Yes |
No |
No |
Persistent Connections |
Yes |
Yes |
Yes |
API supports Charsets |
Yes |
Yes |
Yes |
API supports server-side Prepared Statements |
Yes |
Yes |
No |
API supports client-side Prepared Statements |
No |
Yes |
No |
API supports Stored Procedures |
Yes |
Yes |
No |
API supports Multiple Statements |
Yes |
Most |
No |
API supports Transactions |
Yes |
Yes |
No |
Transactions can be controlled with SQL |
Yes |
Yes |
Yes |
Supports all MySQL 5.1+ functionality |
Yes |
Most |
No |
It's obvious that you should not use MySql as it is already deprecated.
Still can't choose between the API's?
Below is what i can say for each API's.
- MySql
- Not Recommended. Already deprecated. Do not Use this.
- It's already the year 2020.. Please choose the other 2. :D
- MySqli
- Can do asynchronous request to Database. Execute 2 or more queries without each one blocking each other.
- More complex and easier code syntax. Easier to learn.
- PDO
- Better Error Handling.
- Better Parameter Binding.
- Can also be used in other Databases other than MySql. Ex. MsSql, Oracle, PostreSQL, etc...
It is highly recommended to use MySqli or PDO
Of course both MySqli and PDO are already objects but you might need to create wrappers for easier access.
Here's a wrapper for Creating a PHP PDO connection class or Creating a MySqli Database Class in PHP
Replies (0)
Reply