Accessing MySQL Using PHP

Because this book is about MySQL, this chapter focuses on how to use PHP to access a MySQL database. Since the release of MySQL 4.1, there have been two PHP libraries that you can use: the original MySQL library and the MySQL Improved (MySQLi) library.

This creates a dilemma: which library should you use? If you’re working on legacy code or a MySQL server older than version 4.1, you may not have a choice and will need to use the original library. If you’re developing new code, you do have a choice: you can go with the original MySQL library that most developers still understand and use, or the MySQLi library that has additional features and better performance. We recommend that you use the new library for new code, but you should also learn about the older library because you’re likely to encounter it as you develop and modify PHP code.

The Original PHP MySQL Library

This section describes the original PHP library designed for MySQL versions earlier than 4.1. In most PHP installations, it works with later versions, although you can’t take advantage of some newer MySQL features.

In Chapter 13, we showed you a simple PHP code example that uses the original MySQL library. It’s reproduced in Example 14-2.

Example 14-2. Querying the music database with PHP

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html401/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" /> <title>Artists</title> ...

Get Learning MySQL 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.