Let’s face it, the default Php MySQL interfaces are pretty weak. A few of the shortcomings:
- No prepared statements
- In lacking prepared statements, requires the use of escaping functions for any SQL containing POST/GET variables
- Read functions overwrite duplicate column names in returned data when reading rows from joined tables
- Has a very verbose API, with many long parameter lists
- Is not class based, so everyone bakes their own abstraction
Compared to the Perl DBI, for example, Php’s mysql_ functions are absolutely horrid. Luckily there are many great alternatives, as I discovered this weekend with a bit of research.
The PDO
Php’s PDO functions are similar to Perl’s DBI, sporting a reasonable API and supporting a number of databases.
Php’s improved MySQL functions
Php has an improved set of MySQL functions (the mysqli_ functions) that add both a simple class wrapper and a parallel, improved API. The new API supports prepared statements, and has a cleaner interface layout. The new APIs still munge the fetch_assoc functions (they still overwrite), but are otherwise a great improvement.
The improved MySQL functions are only available in Php 5, and I’ve read warnings that it’s a pain to run both the msql_ and mysqli_ functions from one Php installation.
Pear’s MDB2
The MDB2 package from the Pear repository replaces their previous DB package, and provides a similar interface to Perl’s DBI. I find the interface a bit bigger than my needs present, but it is a very complete and mature abstraction.
In the real world
All of these libraries are available on most platforms and from most reasonable webhosts (like Dreamhost).
My first choice so far is the improved mysqli_ class/functions, then the built-in PDO library. I’ll spend a few weeks with them and then post a follow-up.