Learning PHP Data Objects: A Beginner's Guide to PHP Data Objects, Database Connection Abstraction Library for PHP 5

Learning PHP Data Objects: A Beginner's Guide to PHP Data Objects, Database Connection Abstraction Library for PHP 5

Dennis Popel

Language: English

Pages: 116

ISBN: 2:00097487

Format: PDF / Kindle (mobi) / ePub

This book starts with an overview of PHP Data Objects (PDO), followed by getting started with PDO. Then it covers error handling, prepared statements, and handling rowsets, before covering advanced uses of PDO and an example of its use in an MVC application. Finally an appendix covers the new object-oriented features of PHP 5. This book will guide you through the data layer abstraction objects in PHP. PHP developers who need to use PDO for data abstraction.

















Year Summary

fetch()) { ?>

was modified to provide additional functionality that relies on transactions. We also looked at the organization of the transaction-aware code. However, as you might have noticed, we were mixing code that updates databases, processes user input, and renders pages in one file. While we tried to keep the input processing and presentation in different parts of one file (first data processing, then page rendering), we could not separate data processing. In the next chapter, we will see how to

COUNT(*) FROM books"; $q = self::getConn()->query($sql); $rv = $q->fetchColumn(); $q->closeCursor(); return $rv; } /** *This method returns a book with given ID * @param int $id * @return Book */ static function getBook($id) { $id = (int)$id; $sql = "SELECT * FROM books WHERE id=$id"; $q = self::getConn()->query($sql); $rv = $q->fetchObject('Book'); $q->closeCursor(); return $rv; } /** * This method returns the list of all authors * @return PDOStatement */ static function getAuthors() { $sql =

arrays indexed both by column name and number. (This behavior is similar to mysql_fetch_array(), sqlite_fetch_array() without the second parameter, or pg_fetch_array().) We will discuss the fetch styles that PDO has to offer in Chapter 2. The last example was not intended to be used to render HTML pages as it used the newline character to separate lines of output. To use it in a real webpage, you will have to change echo $r['make'], "\n"; to echo $r['make'], "
\n"; Error Handling

that holds the values for the placeholders. Note how the order of the variables in that array matches the order of the placeholders in the $sql variable. Obviously, the number of elements in the array must be the same as the number of placeholders in the query. You have probably noticed that we are not saving the result of the call to the PDOStatement::execute() method in any variable. This is because the statement object itself is used to access the query results, so that we can complete our

Download sample