PHP Data Objects Wez Furlong. About the author PHP Core Developer since 2001 Author of the Streams layer “King” of PECL Author of most of PDO and its.

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.
UFCE8V-20-3 Information Systems Development 3 (SHAPE HK)
PHP (2) – Functions, Arrays, Databases, and sessions.
Fundamentals, Design, and Implementation, 9/e Chapter 12 ODBC, OLE DB, ADO, and ASP.
Fundamentals, Design, and Implementation, 9/e COS 346 DAY 22.
Objectives Connect to MySQL from PHP
DT228/3 Web Development Databases. Database Almost all web application on the net access a database e.g. shopping sites, message boards, search engines.
An Introduction to PHP Data Objects A Better Way to Interact with Your Database by Rusty Keele.
OLE and ODBC: Taming the Technologies The Third Annual Perl Conference, 1999 Sunday, August 22, 1999Roth Consulting ODBC.
Introducing PHP Data Objects Wez Furlong
PHP Programming. Topics Background and History of PHP Installation Comments in PHP Variables Conditions Loops Functions File Handling Database Handling.
Advance Computer Programming Java Database Connectivity (JDBC) – In order to connect a Java application to a database, you need to use a JDBC driver. –
Dataface API Essentials Steve Hannah Web Lite Solutions Corp.
1 Java Database Connection (JDBC) There are many industrial-strength DBMS's commercially available in the market. Oracle, DB2, and Sybase are just a few.
Open Source Server Side Scripting ECA 236 Open Source Server Side Scripting Cookies & Sessions.
SQLite and PHP Wez Furlong Marcus Boerger LinuxTag 2003 Karlsruhe.
PDO, PHP Data Object Use a Database with PHP
4-1 INTERNET DATABASE CONNECTOR Colorado Technical University IT420 Tim Peterson.
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.
PHP With the New PDO::Informix Presented by: Thomas Beebe Advanced DataTools Corporation
PHP Workshop ‹#› PHP Error Handling. PHP Workshop ‹#› Types There are 12 unique error types, which can be grouped into 3 main categories: Informational.
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,
Chapter 6 PHP Interacts with Mysql Database. Introduction In PHP, there is no consolidated interface. Instead, a set of library functions are provided.
PHP Part 2.
1. Connecting database from PHP 2. Sending query 3. Fetching data 4. Persistent connections 5. Best practices.
Oracle SQL Query and Transactions By Shyam Gurram.
Lecture 10 – MYSQL and PHP (Part 2)
Object Oriented Programming in PHP. Topics Quick OOP Review Classes Magic Methods Static Methods Inheritance Exceptions Interfaces Operators Type Hinting.
(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.
JDBC Java and Databases, including Postgress. JDBC l Developed by Industry leaders l Three main goals: –JDBC should be an SQL-level API –JDBC should capitalize.
JSTL Lec Umair©2006, All rights reserved JSTL (ni) Acronym of  JavaServer Pages Standard Tag Library JSTL (like JSP) is a specification, not an.
Object Oriented Programming in PHP. List of Items of Interest What is a Class What is an Object What is a Singleton Multiple Objects, How to store. Advantages.
Session 7: Getting PHP to Talk to MySQL. Objectives Connecting to the Database Building & Executing the SQL SELECT Query Fetching & Displaying the data.
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.
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.
Li Tak Sing COMPS311F. Database programming JDBC (Java Database Connectivity) Java version of ODBC (Open Database Connectivity) ODBC provides a standard.
PHP Workshop ‹#› أطلق إبداعك 2 أطلق إبداعك 2 مدرس معتمد من مركز زووم PHP Data Object (PDO)
JDBC and SQLJ CIS 612 Spring JDBC JDBC is an API that enables database access from Java programs JDBC for DB access provides ◦ Portability across.
PHP Programming. Topics Database Handling (MySQL, MSSQL, ODBC)
Database Access Using JDBC BCIS 3680 Enterprise Programming.
PHP Database Pemrograman Internet. PHP MySQL Database With PHP, you can connect to and manipulate databases. MySQL is the most popular database system.
8 Chapter Eight Server-side Scripts. 8 Chapter Objectives Create dynamic Web pages that retrieve and display database data using Active Server Pages Process.
Chapter 8 Manipulating MySQL Databases with PHP PHP Programming with MySQL 2 nd Edition.
CSI 3125, Preliminaries, page 1 JDBC. CSI 3125, Preliminaries, page 2 JDBC JDBC stands for Java Database Connectivity, which is a standard Java API (application.
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.
DBI: The Neophyte's Guide1 What is DBI? DBI = DataBase Interface DBI is database-independent DBI allows you to write code that interacts with databases.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 14 Web Database Programming Using PHP.
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.
Intro to JDBC Joseph Sant Applied Computing and Engineering Sciences Sheridan ITAL.
CS 440 Database Management Systems Stored procedures & OR mapping 1.
PDOStatement Named Placeholders CIT336 - Connor Wiseman cit336.saveandquit.net/presentation.
JDBC. Database is used to store data permanently. These days almost all Applications needs database to store its data persistently. Below are the most.
Introduction to PHP 5 and IBM DB2 Building web applications with PHP 5.2 and IBM DB2.
Web Systems & Technologies
PHP Data Objects for MySQL
DEPTT. OF COMP. SC & APPLICATIONS
Multitier Architecture, MySQL & PHP
ISC440: Web Programming 2 Server-side Scripting PHP 3
Tutorial 6 PHP & MySQL Li Xu
Presentation transcript:

PHP Data Objects Wez Furlong

About the author PHP Core Developer since 2001 Author of the Streams layer “King” of PECL Author of most of PDO and its drivers Day-job is developing the Fastest MTA on Earth

Coming up… Problems with PHP DB access PDO Solution Features / Drivers Installation Getting Started Features

The Problem No consistency of API between DB extensions Sometimes no self-consistency within a given extension Duplicated code (but not) High maintenance

The PDO Solution Move PHP specific stuff into one extension Database specific stuff (only) in their own extensions Data access abstraction, not database abstraction

Features Performance Native C code beats a scripted solution Takes advantage of latest PHP 5 internals

Features Power Gives you common DB features as a base Still be able to access specialist functions

What can it do? Prepare/execute, bound parameters Transactions LOBS Scrollable Cursors SQLSTATE error codes, flexible error handling Portability attributes

Available Drivers Pretty much all of them: MySQL, PostgreSQL ODBC, DB2, OCI SQLite (2 and 3) Sybase/FreeTDS/MSSQL Firebird (needs love)

Installing Ships with PHP 5.1 Available for PHP 5.0.x via PECL DLLs downloadable for both on Windows via

Building w/ PHP 5.0./configure \ --with-zlib \ --prefix=/usr/local/php5 pear upgrade pear pear install PDO extension=pdo.so in your php.ini

Building w/ PHP 5.0 Select the driver(s) you need pear install PDO_XXX extension=pdo_xxx.so in your php.ini

Installing on Win32 Grab the DLLs from the snaps site You need: –php_pdo.dll –php_pdo_XXX.dll Put them in C:\php5\ext

Switching it on Need to enable PDO in your php.ini MUST load PDO first Unix: –extension=pdo.so –extension=pdo_XXX.so Windows: –extension=php_pdo.dll –extension=php_pdo_XXX.dll

Connecting via PDO try { $dbh = new PDO($dsn, $user, $password, $options); } catch (PDOException $e) { echo ”Failed to connect:”. $e->getMessage(); }

DSNs In general drivername:

DSNs mysql:host=name;dbname=dbname pgsql:native_pgsql_connection_string odbc:odbc_dsn oci:dbname=dbname;charset=charset

DSNs sqlite:/path/to/db/file sqlite::memory: sqlite2:/path/to/sqlite2/file sqlite2::memory:

DSN Aliasing uri:uri –Specify location of a file containing actual DSN on the first line –Works with streams interface, so remote URLs can work too name (with no colon) –Maps to pdo.dsn.name in your php.ini –pdo.dsn.name=sqlite:/path/to/name.db

DSN Aliasing pdo.dsn.name=sqlite:/path/to/name.db $dbh = new PDO("name"); Same as: $dbh = new PDO("sqlite:/path/to/name.db");

Connection Management try { $dbh = new PDO($dsn, $user, $pw); // use the database here //... // done; release the connection $dbh = null; } catch (PDOException $e) { echo ”connect failed:”. $e->getMessage(); }

Persistent PDO Connection stays alive between requests $dbh = new PDO($dsn, $user, $pass, array( PDO_ATTR_PERSISTENT => true ) );

Persistent PDO Can specify your own cache key $dbh = new PDO($dsn, $user, $pass, array( PDO_ATTR_PERSISTENT => “my-key” ) ); Useful for keeping separate persistent connections

Persistent PDO The ODBC driver runs with connection pooling enabled by default “better” than PHP-level persistence –Pool is shared at the process level Can be forced off by setting –pdo_odbc.connection_pooling=off

Let’s get data $dbh = new PDO($dsn); $stmt = $dbh->prepare( "SELECT * FROM FOO"); $stmt->execute(); while ($row = $stmt->fetch()) { print_r($row); } $stmt = null;

Forward-only cursors Also known as "unbuffered" queries in mysql parlance They are the default cursor type rowCount() doesn't have meaning FAST!

Forward-only cursors Other queries are likely to block You must fetch all remaining data before launching another query $stmt->closeCursor()

Buffered Queries $dbh = new PDO($dsn); $stmt = $dbh->query( "SELECT * FROM FOO"); $rows = $stmt->fetchAll(); $count = count($rows); foreach ($rows as $row) { print_r($row); } $stmt = null;

Data typing Very loose uses strings for data Gives you more control over data conversion Supports integers where 1:1 mapping exists Is float agnostic –PDO is precise

Fetch modes $stmt->fetch(PDO_FETCH_BOTH) –Array with numeric and string keys –default option PDO_FETCH_NUM –Numeric only PDO_FETCH_ASSOC –String only

Fetch modes PDO_FETCH_OBJ –stdClass object –$obj->name == ‘name’ column PDO_FETCH_CLASS –You choose the class PDO_FETCH_INTO –You provide the object

Fetch modes PDO_FETCH_COLUMN –Fetches only a particular column PDO_FETCH_BOUND –Only fetches into bound variables PDO_FETCH_FUNC –Returns the result of a callback and more…

Iterators $dbh = new PDO($dsn); $stmt = $dbh->query( "SELECT name FROM FOO", PDO_FETCH_COLUMN, 0); foreach ($stmt as $name) { echo "Name: $name\n"; } $stmt = null;

Changing data $deleted = $dbh->exec( "DELETE FROM FOO WHERE 1"); $changes = $dbh->exec( "UPDATE FOO SET active=1 ". "WHERE NAME LIKE '%joe%'");

Autonumber/sequences $dbh->exec( "insert into foo values (...)"); echo $dbh->lastInsertId(); $dbh->exec( "insert into foo values (...)"); echo $dbh->lastInsertId(“seqname”); It’s up to you to call it appropriately

Smarter queries Quoting is annoying, but essential PDO offers a better way $stmt->prepare(“INSERT INTO CREDITS ”.“(extension, name) ”.“VALUES (:extension, :name)”); $stmt->execute(array( 'extension' => 'xdebug', 'name' => 'Derick Rethans' ));

Smarter queries Quoting is annoying, but essential PDO offers a better way $stmt->prepare(“INSERT INTO CREDITS ”.“(extension, name) ”.“VALUES (?, ?)”); $stmt->execute(array( 'xdebug', 'Derick Rethans' ));

$db->quote() If you still need to quote things “by- hand” Currently a no-op for the ODBC driver

Stored Procedures $stmt = $dbh->prepare( "CALL sp_set_string(?)"); $stmt->bindParam(1, $str); $str = ‘foo’; $stmt->execute();

OUT parameters $stmt = $dbh->prepare( "CALL sp_get_string(?)"); $stmt->bindParam(1, $ret, PDO_PARAM_STR, 4000); if ($stmt->execute()) { echo "Got $ret\n"; }

IN/OUT parameters $stmt = $dbh->prepare( $val = "My Input Data"; $stmt->bindParam(1, $val, PDO_PARAM_STR| PDO_PARAM_INPUT_OUTPUT, 4000); if ($stmt->execute()) { echo "Got $val\n"; }

Binding columns for output $stmt = $dbh->prepare( "SELECT extension, name from CREDITS"); if ($stmt->execute()) { $stmt->bindColumn('extension', $extension); $stmt->bindColumn('name', $name); while ($stmt->fetch(PDO_FETCH_BOUND)) { echo "Extension: $extension\n"; echo "Author: $name\n"; }

Portability Aids PDO aims to make it easier to write db independent apps Number of hacks^Wtweaks for this purpose

Oracle style NULLs Oracle translates empty strings into NULLs $dbh->setAttribute(PDO_ATTR_ORACLE_NULLS, true); Translates empty strings into NULLs when fetching data But won’t change them on insert

Case folding The ANSI SQL standard says that column names are returned in upper case High end databases (eg: Oracle and DB2) respect this Most others don’t $dbh->setAttribute( PDO_ATTR_CASE,PDO_CASE_UPPER);

Error Handling PDO offers 3 different error modes $dbh->setAttribute(PDO_ATTR_ERRMODE, $mode); –PDO_ERRMODE_SILENT –PDO_ERRMODE_WARNING –PDO_ERRMODE_EXCEPTION Attempts to map native codes to SQLSTATE standard codes But still offers native info too

PDO_ERRMODE_SILENT if (!$dbh->query($sql)) { echo $dbh->errorCode(). " "; $info = $dbh->errorInfo(); // $info[0] == $dbh->errorCode() // SQLSTATE error code // $info[1] is the driver specific // error code // $info[2] is the driver specific // error string }

PDO_ERRMODE_WARNING Same as PDO_ERRMODE_SILENT But, raises an E_WARNING as errors are detected You can selectively suppress the warnings operator

PDO_ERRMODE_EXCEPTION try { $dbh->exec($sql); } catch (PDOException $e) { // display warning message print $e->getMessage(); $info = $e->errorInfo; // $info[0] == $e->code; // SQLSTATE error code // $info[1] is the driver specific error code // $info[2] is the driver specific error string }

Transactions $dbh->beginTransaction(); try { $dbh->query("UPDATE..."); $dbh->commit(); } catch (Exception $e) { $dbh->rollBack(); }

LOBs via Streams Large objects are usually 4kb or more in size Advantageous to avoid fetching them until you really need to Mature RDBMS offer LOB APIs for this PDO exposes LOBS as Streams

Fetching an image $db = new PDO($dsn); $stmt = $db->prepare( “select contenttype, imagedata”.“ from images where id=?”); $stmt->execute(array($_GET['id'])); list($type, $lob) = $stmt->fetch(); header("Content-Type: $type"); fpassthru($lob);

Inserting an image $db = new PDO($dsn); $stmt = $db->prepare( “insert into images ”.“(id, contenttype, imagedata) ”.“values (?, ?, ?)”); $id = get_new_id(); $fp = fopen($_FILES['file']['tmp_name'], 'rb'); $stmt->bindParam(1, $id); $stmt->bindParam(2,$_FILES['file']['type']); $stmt->bindParam(3, $fp, PDO_PARAM_LOB); $stmt->execute();

Scrollable Cursors Allow random access to a rowset Higher resource usage than forward-only cursors Can be used to emulate limit, offset style paged queries Can be used for positioned updates (more useful for CLI/GUI apps)

Positioned updates An open (scrollable) cursor can be used to target a row for another query Name your cursor by setting PDO_ATTR_CURSOR_NAME during prepare() UPDATE foo set bar = ? WHERE CURRENT OF cursor_name

Multi-rowset queries $stmt = $dbh->query( "call sp_multi_results()"); do { while($row = $stmt->fetch()) { print_r($row); } } while ($stmt->nextRowset());

Questions?

Resources The PHP Manual: Publications –www-128.ibm.com/developerworks/db2/library/techarticle/ dm-0505furlong/ – otn_pdo_oracle5.html My blog: Bugs: