An Introduction to PHP Data Objects A Better Way to Interact with Your Database by Rusty Keele.

Slides:



Advertisements
Similar presentations
Connecting to Databases. connecting to DB DB server typically a standalone application Server runs on localhost for smaller sites –i.e. Same machine as.
Advertisements

Connecting to Databases. relational databases tables and relations accessed using SQL database -specific functionality –transaction processing commit.
Introduction The concept of “SQL Injection”
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 13 Introduction to SQL Programming Techniques.
Introduction to Database Processing with ADO.NET.
PHP and MySQL. Why Use a Database  Easy access to data  Simultaneous access by multiple users is handled properly  Security - easy to control access.
Session-01. Hibernate Framework ? Why we use Hibernate ?
NOTE: To change the image on this slide, select the picture and delete it. Then click the Pictures icon in the placeholder to insert your own image. Browser.
Introducing PHP Data Objects Wez Furlong
Check That Input Preventing SQL Injection Attacks By Andrew Morton For CS 410.
Databases with PHP A quick introduction. Y’all know SQL and Databases  You put data in  You get data out  You can do processing on it very easily 
Advanced Java New York University School of Continuing and Professional Studies.
LIS651 lecture 7 PHP mySQL Thomas Krichel
PDO, PHP Data Object Use a Database with PHP
Testing. What is Testing? Definition: exercising a program under controlled conditions and verifying the results Purpose is to detect program defects.
CSCI 6962: Server-side Design and Programming JDBC Database Programming.
Chapter 7 PHP Interacts with Ms. Access (Open DataBase Connectivity (ODBC))
PHP Data Object (PDO) Khaled Al-Sham’aa. What is PDO? PDO is a PHP extension to formalise PHP's database connections by creating a uniform interface.
PHP Data Objects Layer (PDO) Ilia Alshanetsky. What is PDO Common interface to any number of database systems. Common interface to any number of database.
Sayed Ahmed Computer Engineering, BUET, Bangladesh MSC, Computer Science, U of Manitoba, Canada
Sys Prog & Scripting - HW Univ1 Systems Programming & Scripting Lecture 19: Database Support.
1 PHP and MySQL. 2 Topics  Querying Data with PHP  User-Driven Querying  Writing Data with PHP and MySQL PHP and MySQL.
Ajax and Ruby on Rails Session 9 INFM 603.
Development Strategies for Web Applications Jonathan Babbage National Superconducting Cyclotron Laboratory.
Nael Alian Introduction to PHP
Codeigniter is an open source web application. It occupies a very small amount of space in the memory and is most useful for developers who aim to develop.
SQL pepper. Why SQL File I/O is a great deal of code Optimal file organization and indexing is critical and a great deal of code and theory implementation.
Accessing MySQL with PHP IDIA 618 Fall 2014 Bridget M. Blodgett.
What is MySQLi? Since the mid-90s, Mysql extension has served as the major bridge between PHP and MySQL. Although it has performed its duty quite well,
Introduction to LINQ Lecture # 19 August Introduction How do you interrogate/manipulate data? What if you could do the work in a type-safe," string-free.
Chapter 6 PHP Interacts with Mysql Database. Introduction In PHP, there is no consolidated interface. Instead, a set of library functions are provided.
Lecture 10 – MYSQL and PHP (Part 2)
(Chapter 10 continued) Our examples feature MySQL as the database engine. It's open source and free. It's fully featured. And it's platform independent.
SQL Server User Group Meeting Reporting Services Tips & Tricks Presented by Jason Buck of Custom Business Solutions.
Open Source Software Unit – 3 Presented By Mr. R.Aravindhan.
JSTL Lec Umair©2006, All rights reserved JSTL (ni) Acronym of  JavaServer Pages Standard Tag Library JSTL (like JSP) is a specification, not an.
Database APIs and Wrappers
Accessing Your MySQL Database from the Web with PHP (Ch 11) 1.
FEN Introduction to the database field:  Applications, concepts and terminology Seminar: Introduction to relational databases.
CHAPTER 9 PHP AND MYSQL. A POSSIBLE SITE CONFIGURATION Application Folder index.php includes (folder)header.phpfooter.phpstyle.cssmodel (folder)mysqli_connect.php.
PHP PDO & PHP SOAP Introduce. Agenda What is PHP PDO and PHP SOAP? Setup PHP PDO to connect database, query database and close the connection. Setup SOAP.
Just a Little PHP Programming PHP on the Server. Common Programming Language Features Comments Data Types Variable Declarations Expressions Flow of Control.
SQL Injection Jason Dunn. SQL Overview Structured Query Language For use with Databases Purpose is to retrieve information Main Statements Select Insert.
Course FAQ’s I do not have any knowledge on SQL concepts or Database Testing. Will this course helps me to get through all the concepts? What kind of.
PHP Workshop ‹#› PHP Data Object (PDO). PHP Workshop ‹#› What is PDO? PDO is a PHP extension to formalise PHP's database connections by creating a uniform.
PHP Workshop ‹#› أطلق إبداعك 2 أطلق إبداعك 2 مدرس معتمد من مركز زووم PHP Data Object (PDO)
PHP, Databases, and Cookies Dave Pease IDS496 12/2/2003
CHAPTER 10 PHP MySQL Database
Mr. Justin “JET” Turner CSCI 3000 – Fall 2015 CRN Section A – TR 9:30-10:45 CRN – Section B – TR 5:30-6:45.
8 th Semester, Batch 2009 Department Of Computer Science SSUET.
Working With Database Library And Helpers. Connecting to your Database First you need to set parameters in you database.php file residing in config folder.
Fundamentals of Web DevelopmentRandy Connolly and Ricardo HoarFundamentals of Web DevelopmentRandy Connolly and Ricardo Hoar Fundamentals of Web DevelopmentRandy.
Spring JDBC Dima Ionut Daniel. Contents What is Spring JDBC? Overview Spring JDBC Core SQL Exceptions Database Connection Batch Operations Handling BLOB/CLOB.
Singleton Academy, Pune. Course syllabus Singleton Academy Pune – Course Syllabus1.
Intro to JDBC Joseph Sant Applied Computing and Engineering Sciences Sheridan ITAL.
SQL pepper. Why SQL File I/O is a great deal of code Optimal file organization and indexing is critical and a great deal of code and theory implementation.
PDOStatement Named Placeholders CIT336 - Connor Wiseman cit336.saveandquit.net/presentation.
Copyright © 2016 Ramez Elmasri and Shamkant B. Navathe.
JDBC.
Introduction to Database Programming with Python Gary Stewart
PHP is a server scripting language, and a powerful tool for making dynamic and interactive Web pages. PHP is a widely-used, free, and efficient alternative.
Web Systems & Technologies
PDO Database Connections
Introduction to Database Processing with ADO.NET
PHP Training at GoLogica in Bangalore
Multitier Architecture, MySQL & PHP
CMPE419 Mobile Application Development
PHP Forms and Databases.
CMPE419 Mobile Application Development
Presentation transcript:

An Introduction to PHP Data Objects A Better Way to Interact with Your Database by Rusty Keele

This Presentation Is For People Who… Want to learn the basics of PHP Data Objects (PDO) Want to start using PDO in their code Have a basic knowledge of PHP and SQL

What We’ll Cover My story An overview of PDO Examples and real PHP code Wrap up

My StoryMy Story

My History with Programming Started on a Commodore 64 – in 1982! B.A. in Computer Science from Weber State University 9 years as a full time programmer – mostly PHP Worked for Utah Education Network the last 4 years

How I Got Started With PDO What I inherited at UEN The mysql_x( ) revelation Switching from MySQL to PostgreSQL ?! 2 birds with 1 stone

PDO: An Overview

What Is PDO? “The PHP Data Objects (PDO) extension defines a lightweight, consistent interface for accessing databases in PHP.” - php.net Is: a data-access abstraction layer Is not: a database abstraction layer Is not: an object relation mapping (ORM) system Does not: rewrite SQL Does not: emulate missing features

Why Use PDO? A future change of database Prepared statements Rewriting code Object oriented programming You’re writing your own framework! ;-)

First Steps PDO is enabled by default as of PHP PDO documentation: php.net/manual/en/book.pdo.php php.net/manual/en/book.pdo.php

The Main PDO Classes PDO : Represents a connection between PHP and a database server. PDOStatement : Represents a prepared statement and, after the statement is executed, an associated result set. PDOException : Represents an error raised by PDO.

Other Important Ideas PDO drivers drivers implement the PDO interface for specific databases MySQL, PostgreSQL, MS SQL Server, Oracle… Prepared statements a kind of compiled template for the SQL that an application runs can be customized using variable parameters prepare once – execute multiple times don't need to be quoted – which helps defend against SQL injection!

Using PDOUsing PDO

Connecting To A Database Connect Close Handle errors

Prepared Statements: Reading Data Using named placeholders Using question mark placeholders, variables, data type and length

Prepared Statements: Inserting Data Binding an array when executing and getting last insert ID

Deleting Data Using exec( ) to get number of rows affected Differences between execute( ) and exec( ) exec( ) A method of the PDO class Executes an SQL statement and returns the number of affected rows execute( ) A method of the PDOStatement class Executes a prepared statement and returns TRUE or FALSE

Putting It All Together

Gotchas! Be careful when using “LIKE %keyword%”

Support For Transactions Only works with databases that support transactions Transactions are typically implemented by "saving-up" your batch of changes to be applied all at once Can roll back on errors

Changing Your Database In theory, you can just switch the Data Source Name in the connection string From: mysql:host=localhost;dbname=testdb;user=un;password=pw To: pgsql:host=localhost;dbname=testdb;user=un;password=pw Watch out for non-standard SQL! For example: MySQL’s “replace into”

PDO And Frameworks Not really needed because of abstraction layers Especially if using ORM CodeIgniter has a PDO driver option in version 3.0 Check the documentation for your framework: Laravel, CakePHP, Zend 2

Wrapping Things Up

The Rest Of The Story Spent a year working on this at UEN (the MySQL to PDO part)… one file at a time! Now I have better, non-deprecated, more secure, uncoupled, object oriented code for database connections – whew! Next: change the back end database to PostgreSQL!

Where To Learn More About PDO PHP’s own documentation: A good intro article: data-objects http:// data-objects Books (on Amazon.com) Learning PHP Data Objects: A Beginner's Guide to PHP Data Objects, Database Connection Abstraction Library for PHP 5 PHP Data Objects Quick Start

Picture Credits All photos from All photos under Creative Commons License Wolf by alexandre alacchi, Wolf Park by Debs, Wolf looking at me by Tambako The Jaguar, Pretty standing wolf by Tambako The Jaguar, Wolves fighting over food by Tambako The Jaguar, Wolf by Jimmy Álvarez, Wolf by Cloudtail, Black Wolf by numb - El Condor, C'est un garçon by peupleloup Commodore 64 by Steve Petrucelli Darn Itch by Brave Heart cartwheel by Julie

Feedback And Questions Please rate my presentation and give feedback Where to get my slides Questions? Comments?