Chapter 16. Performing Database Queries

Much of the point of PHP is to help you translate between a backend database and its frontend presentation on the web. Data can be viewed, added, removed, and tweaked as a result of your web user's keystrokes and mouse clicks.

For most of this chapter, we restrict ourselves to ways to use PHP to look at the contents of a database without altering it, using only the SELECT statement from SQL and displaying the results in HTML tables. We use a single database example to show different strategies, including some handy reusable functions. Finally, we look at code to create the sample data shown in the display examples, using the INSERT statement.

The two big productivity points from this chapter are:

  • Reuse functions in simple cases. The problem of database table display shows up over and over in database-enabled site design. If the display is not complicated, you should be able to throw the same simple function at the problem rather than reinventing the wheel with each PHP page you write.

  • Choose between techniques in complex cases. You may find yourself wanting to pull out a complex combination of information from different tables (which, of course, is part of the point of using a relational database to begin with). You may not be able to map this onto a simple reusable function, but there aren't that many novel solutions either — get to know the alternatives, and ...

Get PHP6 and MySQL® 6 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.