Page 405 - Beginning PHP 5.3
P. 405
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, in MySQL, and populate the tables with some sample data.
9/21/09 9:11:55 AM
c13.indd 367 9/21/09 9:11:55 AM
c13.indd 367