Learning PHP Data Objects
By Dennis Popel
5/5
()
About this ebook
Related to Learning PHP Data Objects
Related ebooks
PHP 7 Programming Blueprints Rating: 0 out of 5 stars0 ratingsMastering Sass Rating: 0 out of 5 stars0 ratingsLearning PHP 7 Rating: 4 out of 5 stars4/5Apache Solr PHP Integration Rating: 0 out of 5 stars0 ratingsWeb Development with MongoDB and NodeJS - Second Edition Rating: 0 out of 5 stars0 ratingsPersistence in PHP with Doctrine ORM Rating: 0 out of 5 stars0 ratingsLaravel 5 Essentials Rating: 0 out of 5 stars0 ratingsExpress Web Application Development Rating: 3 out of 5 stars3/5Node.js By Example Rating: 2 out of 5 stars2/5MongoDB High Availability Rating: 5 out of 5 stars5/5Learning Behavior-driven Development with JavaScript Rating: 5 out of 5 stars5/5PHP Reactive Programming Rating: 0 out of 5 stars0 ratingsPHP 7 Data Structures and Algorithms Complete Self-Assessment Guide Rating: 0 out of 5 stars0 ratingsResponsive Design High Performance Rating: 0 out of 5 stars0 ratingsLearning Bootstrap Rating: 1 out of 5 stars1/5Learning PHP 7 High Performance Rating: 0 out of 5 stars0 ratingsPHP Error Reporting: How To Do It Right Rating: 0 out of 5 stars0 ratingsInstant SASS CSS How-to Rating: 5 out of 5 stars5/5JavaScript at Scale Rating: 0 out of 5 stars0 ratingsPHP 7 Programming Cookbook Rating: 0 out of 5 stars0 ratingsMastering phpMyAdmin 3.4 for Effective MySQL Management Rating: 0 out of 5 stars0 ratingsModular Programming with PHP 7 Rating: 0 out of 5 stars0 ratingsNode Cookbook: Second Edition Rating: 3 out of 5 stars3/5JavaScript Regular Expressions Rating: 3 out of 5 stars3/5PHP 5 CMS Framework Development - 2nd Edition Rating: 0 out of 5 stars0 ratingsPHP Oracle Web Development: Data processing, Security, Caching, XML, Web Services, and Ajax Rating: 0 out of 5 stars0 ratingsAJAX and PHP: Building Responsive Web Applications Rating: 4 out of 5 stars4/5Microsoft .NET Framework 4.5 Quickstart Cookbook Rating: 0 out of 5 stars0 ratingsGet Programming with JavaScript Next: New features of ECMAScript 2015, 2016, and beyond Rating: 0 out of 5 stars0 ratingsLaravel 5.x Cookbook Rating: 0 out of 5 stars0 ratings
Information Technology For You
Summary of Super-Intelligence From Nick Bostrom Rating: 4 out of 5 stars4/5An Ultimate Guide to Kali Linux for Beginners Rating: 3 out of 5 stars3/5CompTia Security 701: Fundamentals of Security Rating: 0 out of 5 stars0 ratingsLearning Microsoft Endpoint Manager: Unified Endpoint Management with Intune and the Enterprise Mobility + Security Suite Rating: 0 out of 5 stars0 ratingsCreating Online Courses with ChatGPT | A Step-by-Step Guide with Prompt Templates Rating: 4 out of 5 stars4/5CompTIA ITF+ CertMike: Prepare. Practice. Pass the Test! Get Certified!: Exam FC0-U61 Rating: 5 out of 5 stars5/5How to Find a Wolf in Siberia (or, How to Troubleshoot Almost Anything) Rating: 0 out of 5 stars0 ratingsCybersecurity for Beginners : Learn the Fundamentals of Cybersecurity in an Easy, Step-by-Step Guide: 1 Rating: 0 out of 5 stars0 ratingsData Analytics for Beginners: Introduction to Data Analytics Rating: 4 out of 5 stars4/5How to Write Effective Emails at Work Rating: 4 out of 5 stars4/5CODING INTERVIEW: Advanced Methods to Learn and Excel in Coding Interview Rating: 0 out of 5 stars0 ratingsCompTIA A+ CertMike: Prepare. Practice. Pass the Test! Get Certified!: Core 1 Exam 220-1101 Rating: 0 out of 5 stars0 ratingsSupercommunicator: Explaining the Complicated So Anyone Can Understand Rating: 3 out of 5 stars3/5Cyber Security Consultants Playbook Rating: 0 out of 5 stars0 ratingsCOMPUTER SCIENCE FOR ROOKIES Rating: 0 out of 5 stars0 ratingsPractical Ethical Hacking from Scratch Rating: 5 out of 5 stars5/5Spreadsheets To Cubes (Advanced Data Analytics for Small Medium Business): Data Science Rating: 0 out of 5 stars0 ratingsHow To Use Chatgpt: Using Chatgpt To Make Money Online Has Never Been This Simple Rating: 0 out of 5 stars0 ratingsLinux Command Line and Shell Scripting Bible Rating: 3 out of 5 stars3/5The Design Inference Rating: 5 out of 5 stars5/5The Programmer's Brain: What every programmer needs to know about cognition Rating: 5 out of 5 stars5/5ChatGPT: The Future of Intelligent Conversation Rating: 4 out of 5 stars4/5Microsoft Access for Beginners and Intermediates Rating: 0 out of 5 stars0 ratingsArch Linux: Fast and Light! Rating: 3 out of 5 stars3/5
Reviews for Learning PHP Data Objects
1 rating0 reviews
Book preview
Learning PHP Data Objects - Dennis Popel
Table of Contents
Learning PHP Data Objects
Credits
About the Author
About the Reviewers
Preface
What This Book Covers
Who This Book is For
Conventions
Reader Feedback
Customer Support
Downloading the Example Code for the Book
Errata
Questions
1. Introduction
Using PDO
Connecting to the Database
Connection Strings
Issuing SQL Queries, Quoting Parameters, and Handling Result Sets
Error Handling
Prepared Statements
Appropriate Understanding of PDO
Summary
2. Using PHP Data Objects: First Steps
Connection Strings
Creating the Sample Database
The Data Model
Creating the MySQL Database
Designing Our Code
PDO Statements and Result Sets
Retrieving Result Set Metadata
Summary
3. Error Handling
Sources of Errors
Server Software Failure or Overload
Improper Configuration of the Application
Improper Validation of User Input
Inserting a Record with a Duplicate Primary Key or Unique Index Value
Syntax Errors in SQL Statements
Types of Error Handling in PDO
Defining an Error Handling Function
Creating the Edit Book Page
Creating the Edit Author Page
Securing against Uncaught Exceptions
Summary
4. Prepared Statements
Prepared Statements
Positional and Named Placeholders
Prepared Statements and Bound Values
Working with BLOBs
Summary
5. Handling Rowsets
Retrieving the Number of Rows in a Result Set
Limiting the Number of Rows Returned
Using Database-Specific SQL
Processing the Top N Rows Only
Starting at an Arbitrary Offset
Summary
6. Advanced PDO Usage
Setting and Getting Connection Attributes
MySQL Buffered Queries
Connecting Using the Connection Configuration File and php.ini Setting
Getting the List of Available Drivers
Transactions
Summary
7. An Advanced Example
Designing the Model
Modifying the Frontend to Use the Model
Advantages of Separating the Model
Further Thoughts
Finishing Up
A. Introduction to OOP in PHP5
What is Object-Oriented Programming?
The Syntax for Declaring Objects
Constructors
Destructors
The Advantages of OOP
Inheritance
Encapsulation
Polymorphism
Static Properties, Methods, and Class Constants
Exceptions
Summary
Index
Learning PHP Data Objects
Dennis Popel
Learning PHP Data Objects
Copyright © 2007 Packt Publishing
All rights reserved. No part of this book may be reproduced, stored in a retrieval system, or transmitted in any form or by any means, without the prior written permission of the publisher, except in the case of brief quotations embedded in critical articles or reviews.
Every effort has been made in the preparation of this book to ensure the accuracy of the information presented. However, the information contained in this book is sold without warranty, either express or implied. Neither the author, Packt Publishing, nor its dealers or distributors will be held liable for any damages caused or alleged to be caused directly or indirectly by this book.
Packt Publishing has endeavored to provide trademark information about all the companies and products mentioned in this book by the appropriate use of capitals. However, Packt Publishing cannot guarantee the accuracy of this information.
First published: September 2007
Production Reference: 2310807
Published by Packt Publishing Ltd.
32 Lincoln Road
Olton
Birmingham, B27 6PA, UK.
ISBN 978-1-847192-66-0
www.packtpub.com
Cover Image by Derek Heasley (<the_matrix@eircom.net>)
Credits
Author
Dennis Popel
Reviewers
Dinangkur Kundu
Tahmid Munaz
Acquisition Editors
Nanda Padmanabhan
Viraj Joshi
Development Editor
Rashmi Phadnis
Technical Editor
Swapna.V.Verlekar
Project Manager
Abhijeet Deobhakta
Editorial Manager
Dipali Chittar
Project Coordinator
Zenab Ismail Kapasi
Indexer
Bhushan Pangaonkar
Proofreaders
Martin Brooks
Chris Smith
Production Coordinators
Shantanu Zagade
Manjiri Nadkarni
Cover Designer
Shantanu Zagade
About the Author
Dennis Popel is an experienced PHP/PHP 5 developer currently working for an Australian web development company, Motive Media (www.motivemedia.com.au). Serving Sun Microsystems Australia, Luna Park Sydney, Alsco Holdings, and Pine Solutions, among others, Dennis leads company development of proprietary, web-based, software solutions. In his spare time, he runs the www.onphp5.com blog and works on an online RSS aggregator newzmix.com.
Dennis Popel has been developing with PHP for more than 5 years and is experienced in such fields as object-oriented design and MVC. Previously he has worked at Rapid Intelligence, another Australian-based web company, publisher of such popular titles as NationMaster.com, FactBites.com, and Qwika.com. In the past, Dennis was developing proprietary Java applications.
This book is devoted to all the people who introduced and guided me in this wonderful world of information technology.
About the Reviewers
Dinangkur Kundu completed his bachelor's degree in Information Technology from Central Queensland University, Australia. He has been working as a software engineer and network admin—designing, developing, and configuring. He has worked with a variety of 2nd, 3rd, and 4th generation languages. He has worked with flat files, indexed files, hierarchical databases, network databases, and relational databases, several Sun and HP servers to configure small and medium range office networks providing Internet service, Mail service, file share service, network-based printing service, backup service, and implementing several network-based applications. Currently, he works as Chief Technical Officer at Quantumcloud, developing and customizing LAMP- and WAMP-based web services. He enjoys producing high-quality software, web-based solutions, and designing secure network.
I would like to thank my family for supporting and inspiring my ongoing passion for software development and the resultant challenges of life near the bleeding edge. I would also like to thank Mr. Jamil and Mr. Hasin, my close professional mentors and who to this day remain close friends. You can contact me at <dkundu@gmail.com>
Tahmid Munaz is currently working in Relisource Technologies (www.relisource.com) as an SQA Engineer. He is also a volunteer in an association called SQABD (SQA Bangladesh—www.sqabd.com) as a Community Relations Manager. He has experience in conducting QA and Testing training and mentoring freshers for Testing and QA Career paths and Consulting.
He loves to keep in touch with other Technical Communities like—JPGroup, Dot_net_community, PHPExpert, and PHPResource. He is addicted to reading blogs and writing when he gets time. You can visit Tahmid's blog at http://tahmidmunaz.blogspot.com
I would like to thank Hasin, the author of Wordpress Complete
, who always inspired me. Thanks to my friend Mizan, the author of MediaWiki Administrators' Tutorial Guide
, who helped me in my reviewing as it was first time for me. Thanks to the Packt team for giving me the support for this startup, especially to Viraj, Rashmi, and Abhijeet. I really enjoyed reviewing and hope to do better in future. I had heard about the author of this book but had no chance to work together. It was a chance for me to work with him and feel proud to help him make a nice book. I would like to thank my Program Managers who have always helped me to do and learn in better ways: Sahadatul Hakim (Enosis Solutions).
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 <feedback@packtpub.com>, 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