Page 428 - Beginning PHP 5.3
P. 428
Part III: Using PHP in Practice
Setting the PDO::ATTR_ERRMODE attribute to PDO::ERRMODE_EXCEPTION tells PDO to throw exceptions
whenever a database error occurs, as you saw in the previous chapter.
The disconnect() function merely takes a PDO object, stored in $conn , and assigns an empty string to
$conn , thereby destroying the object and closing the connection to the MySQL database.
Building the Member Class
The Member class inherits from the DataObject class you just created. It ’ s responsible for retrieving
records from the members table in the database. The class is relatively straightforward, because a lot of
the work is delegated to the DataObject class.
Save the following code as Member.class.php in your book_club folder:
< ?php
require_once “DataObject.class.php”;
class Member extends DataObject {
protected $data = array(
“id” = > “”,
“username” = > “”,
“password” = > “”,
“firstName” = > “”,
“lastName” = > “”,
“joinDate” = > “”,
“gender” = > “”,
“favoriteGenre” = > “”,
“emailAddress” = > “”,
“otherInterests” = > “”
);
private $_genres = array(
“crime” = > “Crime”,
“horror” = > “Horror”,
“thriller” = > “Thriller”,
“romance” = > “Romance”,
“sciFi” = > “Sci-Fi”,
“adventure” = > “Adventure”,
“nonFiction” = > “Non-Fiction”
);
public static function getMembers( $startRow, $numRows, $order ) {
$conn = parent::connect();
$sql = “SELECT SQL_CALC_FOUND_ROWS * FROM “ . TBL_MEMBERS . “ ORDER BY
$order LIMIT :startRow, :numRows”;
try {
$st = $conn- > prepare( $sql );
$st- > bindValue( “:startRow”, $startRow, PDO::PARAM_INT );
$st- > bindValue( “:numRows”, $numRows, PDO::PARAM_INT );
$st- > execute();
390
9/21/09 9:12:03 AM
c13.indd 390 9/21/09 9:12:03 AM
c13.indd 390