Page 440 - Beginning PHP 5.3
P. 440
Part III: Using PHP in Practice
❑ Next, you took a closer look at the SQL SELECT statement. You learned how to use LIMIT to
restrict the number of rows returned from a query, and how to sort the rows of a result set using
the ORDER BY clause. You saw how to make queries more flexible by using the LIKE and NOT
LIKE operators, and how to use functions such as count() , sum() , min() , max() , and avg() to
summarize columns in a table
❑ Duplicate rows can be a problem in result sets, and you saw how to solve this issue by using the
DISTINCT keyword. You also learned how to group results by a specified column or columns
through the use of GROUP BY clauses
❑ One of the main advantages of a relational database is that you can pull data from more than
one table at a time — a process known as joining tables. You learned how to do this, and also
how to use aliases to make both queries and result sets more readable
❑ To round off the discussion on SELECT queries, you explored a few of the myriad MySQL
operators and functions that you can use to add even more power to your queries
In the second half of the chapter you built a member viewer application that was capable of listing all
the members in the fictional book club database, as well as viewing detailed information about each
member. In the process you worked with abstract classes, saw how to create classes to deal with database
table access, learned some more useful features of PDO such as prepared queries, and discovered how to
use MySQL ’ s SQL_CALC_FOUND_ROWS keyword to help you display table contents over several pages.
You now have a solid grounding in how to construct queries and communicate with MySQL from your
PHP scripts. The next chapter takes things further and looks at how to manipulate data in a database
from within PHP.
Meanwhile, try the following two exercises, which test both your SQL query skills and your PHP
programming skills. You can find the solutions to these exercises in Appendix A.
Exercises
1. Write an SQL query to calculate the total number of page views made by all male visitors to the
book club Web site, as well as the total page views from all female visitors.
2. Referring back to the member viewer application you created in this chapter, modify the Member
class ’ s getMembers() method to allow an optional fourth parameter, $interest . When this
parameter is specified, the method should only return members whose otherInterests fields
contain the string supplied in $interest .
402
9/21/09 9:12:08 AM
c13.indd 402 9/21/09 9:12:08 AM
c13.indd 402