Chapter 7. Understanding the WordPress Database Class

IN THIS CHAPTER

  • Examining the schema and properties of the database class

  • Adding Data to MySQL with WordPress

  • Retrieving data from MySQL with WordPress

  • Preventing SQL injection

If you've been developing Web sites or applications with PHP for any length of time, you are probably familiar with PHP database functions such as mysql_query(), mysql_fetch_array(), and so on. You might also be familiar with database abstraction classes. Like other abstraction classes, the database class used by WordPress provides an abstraction layer between the developer and the raw MySQL functions in PHP.

Note

A database abstraction layer is a class or library that sits between your code and a series of databases. As every database has its own nuances, and the built-in database functions are messy and inconsistent, a database abstraction layer sorts out that messiness by providing a consistent interface to multiple databases.

The database class used by WordPress — ezSQL — is an extremely lightweight MySQL database abstraction class that, like much of WordPress' core, originated elsewhere. The original author, Justin Vincent, still maintains this class at www.woyano.com/jv/ezsql. It can be found in WordPress in /wp-includes/wp-db.php.

Before diving too far in, you should take note that ezSQL is not a database abstraction layer; it is a MySQL abstraction layer. It is possible, however, to take the wp-db.php file and manipulate it to talk to other database systems ...

Get WordPress® Bible now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.