Got a question from one of my student and thought I should share the answer here
Please can you tell me the advantages of using PDO to connect or interact with the database over MySQLi?
Good question Mike, PDO and MySQLi are two excellent API’s for database connection in PHP however the following comparison will help answer your question
PDO currently work on and support 12 different database systems (drivers), whereas MySQLi only work with MySQL databases.
This implies that if you’re switching your project to another database system lets say SQLite, PDO makes the process painless because you will only need to change the Data Source Name (Connection string) and few queries to make the switch but with MySQLi on the other hand you will have to write the entire code including queries again.
PDO have a built in exception class that handles any kind of problems that may occur when processing database queries with MySQLi we need to manually handle exceptions.
Both PDO and MySQLi are object-oriented, however MySQLi also offers an API for procedural approach.
Both PDO and MySQLi support Prepared Statements which helps to protect against SQL injection; this is very important for the security of web applications.
Both PDO and MySQLi have a fairly easy connection method and in terms of performance they are both fast
Note: Using either PDO or MySQLi is a matter of choice, why I encourage the use of PDO for obvious reasons you should use “Whichever is suitable for your project”.
share your thought