Chapter 13. Retrieving Data from MySQL with PHP

Up to now, you've concentrated mainly on connecting to MySQL, either through the command-line tool or through PHP's PDO extension, and on creating tables and filling them with data. One of the first SQL statements you came across in the last chapter was a basic SELECT query. There's quite a lot more you can do with SELECT, and this chapter focuses on the different ways you can use queries in PHP scripts to get at the data stored in a MySQL database.

You start off by creating a couple of MySQL tables for a fictional book club database. These tables are used in the examples and scripts throughout this chapter and the next.

You then take a close look at how to construct SQL SELECT statements so that they access the data you want, arranged in the way you want. You learn how to:

  • Limit the number of results returned

  • Order and group results

  • Query multiple tables at once

  • Use various MySQL functions and other features to build more flexible queries

After exploring the theory of SELECT statements, you create a member viewer application that you can use to access the book club tables you created at the start of the chapter.

Setting Up the Book Club Database

The example queries and scripts in this chapter and the next work with two tables: a members table of book club members, and an accessLog table to track each member's visits to the book club Web site. So that you can work through these examples, first create these tables and a database to hold them, ...

Get Beginning PHP 5.3 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.