Page 423 - Beginning PHP 5.3
P. 423
Chapter 13: Retrieving Data from MySQL with PHP
String functions allow you to manipulate string values, much like MySQL ’ s string functions:
mysql > SELECT substring( ‘Hello, world!’, 2, 4 );
+------------------------------------+
| substring( ‘Hello, world!’, 2, 4 ) |
+------------------------------------+
| ello |
+------------------------------------+
1 row in set (0.00 sec)
(Note that character positions start from 1 in MySQL, rather than zero.)
MySQL also features many math functions:
mysql > SELECT pow( pi(), 2 );
+-----------------+
| pow( pi(), 2 ) |
+-----------------+
| 9.8696044010894 |
+-----------------+
1 row in set (0.03 sec)
You can see how, through the use of functions, operators, and other constructs, you can actually do a
great deal of data processing within MySQL itself. It ’ s not just about retrieving data.
Creating a Member Record Viewer
Now that you have a basic grounding in how to retrieve data via SQL, it ’ s time to write a data - driven
PHP application. Along the way, you ’ ll delve deeper into the power of PDO, learn some more useful
MySQL features, and exercise your object - oriented programming skills.
This application is relatively simple. First, it displays a list of all the members of the book club as an
HTML table. The table includes columns for username, first name, and last name, and you can sort the
data by any of these columns. The member list is also paged, displaying only five members at once, and
features links at the bottom of the list to let you move forward and backward one page at a time.
Each member in the list includes a View Member link that you can click to view the complete member
record, including the date they joined, their gender, their favorite genre, their email address, their
interests, and the pages that they ’ ve viewed on the book club Web site.
The application is object - oriented, and creates classes to handle the retrieval of member and access log
records from the database. The application is also split across a number of small files. Generally
speaking, this approach is better than having a single large script file to hold all the application code,
because it makes it easier to locate and debug code.
385
9/21/09 9:12:01 AM
c13.indd 385
c13.indd 385 9/21/09 9:12:01 AM