Page 448 - Beginning PHP 5.3
P. 448
Part III: Using PHP in Practice
$st = $conn- > prepare( $sql );
$st- > bindValue( “:emailAddress”, $emailAddress, PDO::PARAM_STR );
$st- > execute();
$row = $st- > fetch();
parent::disconnect( $conn );
if ( $row ) return new Member( $row );
} catch ( PDOException $e ) {
parent::disconnect( $conn );
die( “Query failed: “ . $e- > getMessage() );
}
}
These methods should be self - explanatory. You can see that they work in much the same way as
getMember() , which you created in the previous chapter.
Next, add a short method, getGenres() , that simply retrieves the values in the private $_genres array
property. This will be used for displaying a list of genres for the prospective member to choose from.
Insert it just below the existing getFavoriteGenreString() method in the class file:
public function getGenres() {
return $this- > _genres;
}
So far the class contains methods for retrieving member records from the members table. Now you ’ re
going to add a new method, insert() , that adds a new member to the table. Add the following code to
the end of the class file, just before the curly brace that closes the class:
public function insert() {
$conn = parent::connect();
$sql = “INSERT INTO “ . TBL_MEMBERS . “ (
username,
password,
firstName,
lastName,
joinDate,
gender,
favoriteGenre,
emailAddress,
otherInterests
) VALUES (
:username,
password(:password),
:firstName,
:lastName,
:joinDate,
:gender,
:favoriteGenre,
:emailAddress,
:otherInterests
)”;
try {
410
9/21/09 9:14:05 AM
c14.indd 410 9/21/09 9:14:05 AM
c14.indd 410