25.09.2014 Views

ZEND PHP 5 Certification STUDY GUIDE

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

160 ” Database Programming<br />

While this example uses named placeholders—a named “template” variable preceeded<br />

by a colon (:)—it is also possible to use question mark placeholders.<br />

// Filter input from $_GET<br />

$author1 = ’’;<br />

if (ctype_alpha($_GET[’author1’]))<br />

{<br />

$author1 = $_GET[’author1’];<br />

}<br />

$author2 = ’’;<br />

if (ctype_alpha($_GET[’author2’]))<br />

{<br />

$author2 = $_GET[’author2’];<br />

}<br />

// Set a named placeholder in the SQL statement for author<br />

$sql = ’SELECT author.*, book.* FROM author<br />

LEFT JOIN book ON author.id = book.author_id<br />

WHERE author.last_name = :author’;<br />

$stmt = $dbh->prepare($sql);<br />

$stmt->setFetchMode(PDO::FETCH_OBJ);<br />

// Fetch results for the first author<br />

$stmt->bindParam(’:author’, $author1);<br />

$stmt->execute();<br />

$results = $stmt->fetchAll();<br />

foreach ($results as $row)<br />

{<br />

echo "{$row->title}, {$row->last_name}\n";<br />

}<br />

// Fetch results for the second author<br />

$stmt->bindParam(’:author’, $author2);<br />

$stmt->execute();<br />

Licensed to 482634 - Amber Barrow (itsadmin@deakin.edu.au)<br />

$results = $stmt->fetchAll();<br />

foreach ($results as $row)<br />

{<br />

echo "{$row->title}, {$row->last_name}\n";<br />

}

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!