Nicholas Skinner

Freelance website and web application developer

PHP Database Access: Switching to PDO from “mysql_” functions

PDO PHP Class ScreenshotWhen I first learnt PHP several years ago the most common way to access MySQL databases was through the original MySQL extension (“mysql_” set of functions) that I have been using ever since (through some custom classes). During that time however the original extension has become “Maintenance only” with usage being “strongly” discouraged by the PHP documentation, and “Long term deprecation announced”.

After becoming aware of the original extension becoming out of favour I initially ignored it (in terms of having any practical impact on coding PHP applications using databases), since as with any change it usually takes time for everyone else to catch up (in this case web hosting providers) and limiting my code to only run on certain supported web hosts would not be ideal for clients.

It is however usually a good idea to get ahead of such changes so that old websites / applications are more likely to be naturally replaced rather than requiring updates to be compatible when a change is later forced. Therefore I recently looked into things again, and testing on some of the more well known hosts / control panel software that my clients use, including 1&1, 123-Reg, Pulsant, HostDime, cPanel and finding support for the newer alternatives I started considering my options.

I mainly looked into mysqli and PDO. As far as I could see practically there was not too much difference between them, however favouring object orientated programming (PDO supports only OOP, while mysqli supports both Procedural and OOP style programming), and that PDO also supports other types of database such as SQLite (which seemed like it may be useful in the future for smaller projects) I decided to go with PDO.

[Download: PDO Database Class Library]

Leave a Reply