Book Image

Learning PHP Data Objects

By : Dennis Popel
Book Image

Learning PHP Data Objects

By: Dennis Popel

Overview of this book

PDO is lighter, faster, and more powerful than existing PHP data abstraction interfaces. PDO is a common interface to different databases that must be used with a database-specific PDO driver to access a particular database server: the PDO extension does not provide a database abstraction by itself; it doesn't rewrite SQL, emulate missing database features, or perform any database functions using by itself. It performs the same role as other classic database abstraction layers such as ODBC and JDBC: it's a query abstraction layer that abstracts the mechanism for accessing a database and manipulating the returned records; each database driver that implements the PDO interface can also expose database-specific features as regular extension functions. ¬ PDO ships with PHP 5.1, and is available as an extension for PHP 5.0; it requires the new object-oriented features of PHP 5, and cannot run with earlier versions of PHP.This book will teach you how to use the PDO, including its advanced features. Readers need to be aware of the basics of data abstraction and should be familiar with PHP.
Table of Contents (13 chapters)

Preface

This book will introduce you to one of the most important extensions to PHP that are available, starting with PHP version 5.0—the PHP Data Objects, commonly known as PDO.

PHP grew in to a very popular web programming language due to its simplicity and ease of use. One of the key factors of this growing success is the built-in possibility to access many popular relational database management systems (RDBMS), such as MySQL, PostgreSQL, and SQLite, to name just a few. Today, most of the existing and newly created web applications interconnect with these databases to produce dynamic, data-driven websites.

While most PHP-enabled web servers are still running PHP versions prior to 5.0, the enhancements and performance improvements introduced with this new version will lead to wide acceptance of PHP 5 at all levels during coming years. This imposes the need to start familiarizing ourselves with all the advanced features available in this version today.

What This Book Covers

Chapter 1 gives an overview of PDO along with a few features likes single interface for creating a connection, connection strings, uniform statement methods, and use of exceptions and a singe system of error codes.

Chapter 2 helps to get you started with PDO, by creating a sample database and then by creating a connection object. It also introduces PDOStatement classes.

Chapter 3 deals with various error-handling processes and their uses.

Chapter 4 introduces prepared statements. It deals with using prepared statements without binding values, binding a variable, and binding a parameter to a prepared statement. We also take a look at how to work with BLOBs using streams so that we do not risk query failures.

Chapter 5 helps us determine the number of rows in the returned result set. Also, we come across a new concept—scrollable cursors, which allow us to fetch subsets of rows from a result set.

Chapter 6 talks about advanced uses of PDO and includes setting connection parameters, transactions, and methods of PDO and the PDOStatement class.

Chapter 7 gives an example, where creation of the method part of an MVC application is discussed.

Appendix A explains the object-oriented features like inheritance, encapsulation, polymorphism, and exception handling.

Who This Book is For

This book is targeted at PHP programmers, who are considering migrating to PHP 5 and using the new database connection abstraction library, PHP Data Objects. While PDO is fully object oriented, the familiarity with this programming paradigm is required. Novice users who are not familiar with PHP 5's object-oriented features may consider reading Appendix A first so that they can follow the code examples in this book.

We assume that the reader is familiar with SQL, at the level of creating tables and making simple SELECT queries as well as updates. Our examples are based on MySQL and SQLite databases as these are the most used options and the only ones available at most cheap hosting providers.

At the end of this book we will present a more advanced example which may be of interest to expert programmers with deeper knowledge of SQL and programming concepts.

Conventions

In this book, you will find a number of styles of text that distinguish between different kinds of information. Here are some examples of these styles, and an explanation of their meaning.

There are three styles for code. Code words in text are shown as follows: "PostgreSQL users might have already used pg_prepare() and pg_execute() pair."

A block of code will be set as follows:

// Assume we also want to filter by make
$sql = 'SELECT * FROM cars WHERE make=?';
$stmt = $conn->prepare($sql);
$stmt->execute(array($_REQUEST['make']));

When we wish to draw your attention to a particular part of a code block, the relevant lines or items will be made bold:

// Assume we also want to filter by make
$sql = 'SELECT * FROM cars WHERE make=?';
$stmt = $conn->prepare($sql);

$stmt->execute(array($_REQUEST['make']));

New terms and important words are introduced in a bold-type font. Words that you see on the screen, in menus or dialog boxes for example, appear in our text like this: "You can simply click on the Authors link located on the books listing page in your browser ".

Note

Important notes appear in a box like this.

Note

Tips and tricks appear like this.

Reader Feedback

Feedback from our readers is always welcome. Let us know what you think about this book, what you liked or may have disliked. Reader feedback is important for us to develop titles that you really get the most out of.

To send us general feedback, simply drop an email to , making sure to mention the book title in the subject of your message.

If there is a book that you need and would like to see us publish, please send us a note in the SUGGEST A TITLE form on www.packtpub.com or email .

If there is a topic that you have expertise in and you are interested in either writing or contributing to a book, see our author guide on www.packtpub.com/authors.

Customer Support

Now that you are the proud owner of a Packt book, we have a number of things to help you to get the most from your purchase.

Downloading the Example Code for the Book

Visit http://www.packtpub.com/support, and select this book from the list of titles to download any example code or extra resources for this book. The files available for download will then be displayed.

The downloadable files contain instructions on how to use them.

Errata

Although we have taken every care to ensure the accuracy of our contents, mistakes do happen. If you find a mistake in one of our books—maybe a mistake in text or code—we would be grateful if you would report this to us. By doing this you can save other readers from frustration, and help to improve subsequent versions of this book. If you find any errata, report them by visiting http://www.packtpub.com/support, selecting your book, clicking on the Submit Errata link, and entering the details of your errata. Once your errata are verified, your submission will be accepted and the errata added to the list of existing errata. The existing errata can be viewed by selecting your title from http://www.packtpub.com/support.

Questions

You can contact us at if you are having a problem with some aspect of the book, and we will do our best to address it.