Ready to watch this entire course?
Become a member and get unlimited access to the entire skills library of over 4,974 courses, including more Developer and personalized recommendations.Start Your Free Trial Now
- View Offline
- Connecting to a database with PDO or MySQLi
- Fetching a result set
- Executing simple non-SELECT queries
- Sanitizing user input
- Binding input and output values
- Passing an array of values to the execute() method
- Working with advanced PDO fetch methods
- Executing a MySQLi transaction
- Freeing resources that are no longer needed
- Submitting multiple queries
- Creating an instance of a class from a result set
Skill Level Intermediate
Throughout this course, I've deliberately avoided comparing PDO and MySQL improved, but now we've come to the end of our journey. Maybe this is an appropriate time to consider how they measure up against each other. PDO's main strength, is that the code is database neutral. You need to learn only one API to work with more than a dozen database systems. In theory, just change the DSN, and the same code works with a different database. All the scripts in chapters two, three and four, work seamlessly with SQL Lite and My SQL.
In practice, differences in SQL dialects make conversion more difficult. Also, some PDO drivers are still experimental. So, you need to check with the PHP documentation, to see how mature the driver for your chosen database system is. MySQL improves great strength, is that it's designed specifically to work with MySQL. A simple example's, the data_seek method and num_rows property, make working with MYSQL easier than using PDO.
It's also what MySQL recommends you should use. But being a dedicated API is a weakness. You can't use it with anything other than MySQL or MariaDB. Another point to take into consideration, is that the PDO driver for MySQL, is stable and actively maintained. So using PDO with MySQL, is a viable option. So if you're working with MySQL, which should you choose? If you search the internet for benchmark tests, you'll find conflicting opinions on whether MySQL runs faster with MySQLi, or PDO.
Both MySQLi and PDO access the same underlying driver that interacts directly with the database. So any differences in performance, are likely to be insignificant. WordPress and Joomla, use MySQLi, while Drupal has opted for PDO. And WordPress is also considering adding support for PDO. So there's no clear winner when it comes to working with MySQL or MariaDB. But let's say you opt for MySQLi, and later need to work on a project that requires PDO.
How difficult would it be to make the transition? If you followed both parts of this course, you'll know there are many similarities, but also some potentially confusing differences. Both support question marks as anonymous place holders in prepared statements. PDO also supports named place holders. Personally, I think this makes PDO easier to use. They have similar support for transactions. In PDO, you roll back the individual statements. Whereas in MySQLi, it's the database connection object that does the roll back, perhaps the most confusing aspect is the different spelling of methods, PDO uses camel case whereas MySQLi uses underscores and method names but these are relatively minor issues. Making the switch from MySQLi to PDO should be fairly painless. So, with those thoughts, that concludes this course on accessing databases with object oriented PHP. I hope you found it useful. Thanks for watching, and be sure to check out my other courses here on lynda.com.