SlideShare una empresa de Scribd logo
1 de 46
Descargar para leer sin conexión
PHP Data Objects
         Wez Furlong
   <wez@messagesystems.com>
About me
 •   PHP Core Developer since 2001

 •   Author of the Streams layer

 •   I hold the title “King” of PECL

 •   Author of most of PDO and its drivers
What is PDO?
 •   PHP Data Objects

 •   A set of PHP extensions that provide a core PDO class and database
     specific drivers

 •   Focus on data access abstraction rather than database abstraction
What can it do?
 •   Prepare/execute, bound parameters

 •   Transactions

 •   LOBS

 •   SQLSTATE standard error codes, flexible error handling

 •   Portability attributes to smooth over database specific nuances
What databases are supported?
 •   MySQL, PostgreSQL

 •   ODBC, DB2, OCI

 •   SQLite

 •   Sybase/FreeTDS/MSSQL
Connecting
 try {

     $dbh = new PDO($dsn, $user,
                    $password, $options);

 } catch (PDOException $e) {

     die(“Failed to connect:” .
         $e->getMessage();

 }
DSNs
 •   mysql:host=name;dbname=dbname

 •   pgsql:host=name dbname=dbname

 •   odbc:odbc_dsn

 •   oci:dbname=dbname;charset=charset

 •   sqlite:/path/to/file
Connection Management
 try {

     $dbh = new PDO($dsn, $user, $pw);
     // use the database here
     // ...
     // done; release
     $dbh = null;

 } catch (PDOException $e) {

     die($e->getMessage();

 }
DSN Aliasing
 •   uri:uri

      •   Specify location of a file that contains the actual DSN on the first
          line

      •   Works with the streams interface, so remote URLs can work too
          (this has performance implications)

 •   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”);

  is equivalent to:

  $dbh = new PDO(“sqlite:path/to/name.db”);
Persistent Connections
 // Connection stays alive between requests

 $dbh = new PDO($dsn, $user, $pass,
    array(
      PDO::ATTR_PERSISTENT => true
    )
 );
Persistent Connections
 // 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

  (requires that your web server be restarted)
Error Handling
 •   Maps error codes to ANSI SQLSTATE (5 character text string)

     •   also provides the native db error information

 •   Three error handling strategies

     •   silent (default)

     •   warning

     •   exception
PDO::ERRMODE_SILENT
// The default mode

if (!dbh->query($sql)) {
  echo $dbh->errorCode(), “<br>”;
  $info = $dbh->errorInfo();
  // $info[0] == $dbh->errorCode()
  //             SQLSTATE error code
  // $info[1] is driver specific err code
  // $info[2] is driver specific
  //             error message
}
PDO::ERRMODE_WARNING
$dbh->setAttribute(PDO::ATTR_ERRMODE,
                   PDO::ERRMODE_WARNING);

Behaves the same as silent mode

Raises an E_WARNING as errors are detected

Can suppress with @ operator as usual
PDO::ERRMODE_EXCEPTION
$dbh->setAttribute(PDO::ATTR_ERRMODE,
                    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] driver specific error code
  // $info[2] driver specific error string
}
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
 •   a.k.a. “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);
 }
Data typing
 •   Very loose

 •   Prefers strings

 •   Gives you more control over data conversion
Fetch modes

 • $stmt->fetch(PDO::FETCH_BOTH);
  -   Array with numeric and string keys

  -   default option

 • PDO::FETCH_NUM
  -   numeric keys only

 • PDO::FETCH_ASSOC
  -   string keys 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 a column (example later)
 • PDO::FETCH_BOUND
  - Only fetches into bound variables
 • PDO::FETCH_FUNC
  - Returns the result filtered through a callback
 •   see the manual for more
Iterators
 $dbh = new PDO($dsn);
 $stmt = $dbh->query(
            “SELECT name FROM FOO”,
            PDO::FETCH_COLUMN, 0);
 foreach ($stmt as $name) {
   echo “Name: $namen”;
 }

 $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”);



 Its up to you to call the right one for your db!
Prepared Statements
 // No need to manually quote data here

 $stmt = $dbh->prepare(
    “INSERT INTO CREDITS (extension, name)”
   .“VALUES (:extension, :name)”);

 $stmt->execute(array(
    ‘extension’ => ‘xdebug’,
    ‘name’      => ‘Derick Rethans’
 ));
Prepared Statements
 // No need to manually quote data here

 $stmt = $dbh->prepare(
    “INSERT INTO CREDITS (extension, name)”
   .“VALUES (?, ?)”);

 $stmt->execute(array(
                   ‘xdebug’,
                   ‘Derick Rethans’
 ));
$db->quote()

 • If you really must quote things “by-hand”
 • $db->quote() adds quotes and proper escaping as
   needed
 • But doesn’t do anything in the ODBC driver!
 • Best to use prepared statements
Transactions
 $dbh->beginTransaction();
 try {
   $dbh->query(“UPDATE ...”);
   $dbh->query(“UPDATE ...”);
   $dbh->commit();
 } catch (PDOException $e) {
   $dbh->rollBack();
 }
Stored Procedures
 $stmt = $dbh->prepare(
               “CALL sp_set_string(?)”);
 $stmt->execute(array(‘foo’));



 $stmt = $dbh->prepare(
               “CALL sp_set_string(?)”);

 $stmt->bindValue(1, ‘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 $retn”;
 }
IN/OUT parameters
 $stmt = $dbh->prepare(
            “call @sp_inout(?)”);
 $val = “My input data”;
 $stmt->bindParam(1, $val,
                  PDO::PARAM_STR|
                  PDO::PARAM_INPUT_OUTPUT,
                  4000);
 if ($stmt->execute()) {
   echo “Got $valn”;
 }
Multi-rowset queries
 $stmt = $dbh->query(
           “call sp_multi_results()”);
 do {
   while ($row = $stmt->fetch()) {
      print_r($row);
   }
 } while ($stmt->nextRowset());
Binding columns
 $stmt = $dbh->prepare(
    “SELECT extension, name from CREDITS”);
 if ($stmt->execute()) {
   $stmt->bindColumn(‘extension’, $ext);
   $stmt->bindColumn(‘name’, $name);
   while ($stmt->fetch(PDO::FETCH_BOUND)) {
     echo “Extension: $extn”;
     echo “Author:    $namen”;
   }
 }
Portability Aids
 •   PDO aims to make it easier to write db independent apps

 •   A 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);
LOBs

 • Large objects are usually >4kb in size
 • Nice to avoid fetching them until you need them
 • Mature RDBMS offer LOB APIs for this
 • PDO exposes LOBs as Streams
Fetching an image
 $stmt = $dbh->prepare(
    “select contenttype, imagedata”
   .“ from images where id=?”);
 $stmt->execute(array($_GET[‘id’]));
 $stmt->bindColumn(1, $type,
                   PDO::PARAM_STR, 256);
 $stmt->bindColumn(2, $lob,
                   PDO::PARAM_LOB);
 $stmt->fetch(PDO::FETCH_BOUND);
 header(“Content-Type: $type”);
 fpassthru($lob);
Uploading an image
 $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 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
Questions?
 •   Find these slides on my blog and on slideshare.net

 •   My blog: http://netevil.org/

 •   Gold: http://troels.arvin.dk/db/rdbms/#select-limit-offset

Más contenido relacionado

La actualidad más candente

Chapter 9 - Virtual Memory
Chapter 9 - Virtual MemoryChapter 9 - Virtual Memory
Chapter 9 - Virtual MemoryWayne Jones Jnr
 
Explaining Explain
Explaining ExplainExplaining Explain
Explaining ExplainRobert Treat
 
Page cache in Linux kernel
Page cache in Linux kernelPage cache in Linux kernel
Page cache in Linux kernelAdrian Huang
 
Linux Kernel Booting Process (2) - For NLKB
Linux Kernel Booting Process (2) - For NLKBLinux Kernel Booting Process (2) - For NLKB
Linux Kernel Booting Process (2) - For NLKBshimosawa
 
Vmlinux: anatomy of bzimage and how x86 64 processor is booted
Vmlinux: anatomy of bzimage and how x86 64 processor is bootedVmlinux: anatomy of bzimage and how x86 64 processor is booted
Vmlinux: anatomy of bzimage and how x86 64 processor is bootedAdrian Huang
 
What's new in PHP 8.0?
What's new in PHP 8.0?What's new in PHP 8.0?
What's new in PHP 8.0?Nikita Popov
 
C++ Function
C++ FunctionC++ Function
C++ FunctionHajar
 
Basic Crud In Django
Basic Crud In DjangoBasic Crud In Django
Basic Crud In Djangomcantelon
 
Introduction to PHP - Basics of PHP
Introduction to PHP - Basics of PHPIntroduction to PHP - Basics of PHP
Introduction to PHP - Basics of PHPwahidullah mudaser
 
Reverse Mapping (rmap) in Linux Kernel
Reverse Mapping (rmap) in Linux KernelReverse Mapping (rmap) in Linux Kernel
Reverse Mapping (rmap) in Linux KernelAdrian Huang
 
Process Address Space: The way to create virtual address (page table) of user...
Process Address Space: The way to create virtual address (page table) of user...Process Address Space: The way to create virtual address (page table) of user...
Process Address Space: The way to create virtual address (page table) of user...Adrian Huang
 
Database constraints
Database constraintsDatabase constraints
Database constraintsTony Nguyen
 
COSCUP 2020 RISC-V 32 bit linux highmem porting
COSCUP 2020 RISC-V 32 bit linux highmem portingCOSCUP 2020 RISC-V 32 bit linux highmem porting
COSCUP 2020 RISC-V 32 bit linux highmem portingEric Lin
 
Php famous built in functions
Php   famous built in functionsPhp   famous built in functions
Php famous built in functionsMaaz Shamim
 
Dynamic memory allocation
Dynamic memory allocationDynamic memory allocation
Dynamic memory allocationNaveen Gupta
 
PHP Unit 3 functions_in_php_2
PHP Unit 3 functions_in_php_2PHP Unit 3 functions_in_php_2
PHP Unit 3 functions_in_php_2Kumar
 
Physical Memory Models.pdf
Physical Memory Models.pdfPhysical Memory Models.pdf
Physical Memory Models.pdfAdrian Huang
 
Linux Kernel - Virtual File System
Linux Kernel - Virtual File SystemLinux Kernel - Virtual File System
Linux Kernel - Virtual File SystemAdrian Huang
 
Decompressed vmlinux: linux kernel initialization from page table configurati...
Decompressed vmlinux: linux kernel initialization from page table configurati...Decompressed vmlinux: linux kernel initialization from page table configurati...
Decompressed vmlinux: linux kernel initialization from page table configurati...Adrian Huang
 

La actualidad más candente (20)

Chapter 9 - Virtual Memory
Chapter 9 - Virtual MemoryChapter 9 - Virtual Memory
Chapter 9 - Virtual Memory
 
Explaining Explain
Explaining ExplainExplaining Explain
Explaining Explain
 
Page cache in Linux kernel
Page cache in Linux kernelPage cache in Linux kernel
Page cache in Linux kernel
 
Linux Kernel Booting Process (2) - For NLKB
Linux Kernel Booting Process (2) - For NLKBLinux Kernel Booting Process (2) - For NLKB
Linux Kernel Booting Process (2) - For NLKB
 
Vmlinux: anatomy of bzimage and how x86 64 processor is booted
Vmlinux: anatomy of bzimage and how x86 64 processor is bootedVmlinux: anatomy of bzimage and how x86 64 processor is booted
Vmlinux: anatomy of bzimage and how x86 64 processor is booted
 
Character drivers
Character driversCharacter drivers
Character drivers
 
What's new in PHP 8.0?
What's new in PHP 8.0?What's new in PHP 8.0?
What's new in PHP 8.0?
 
C++ Function
C++ FunctionC++ Function
C++ Function
 
Basic Crud In Django
Basic Crud In DjangoBasic Crud In Django
Basic Crud In Django
 
Introduction to PHP - Basics of PHP
Introduction to PHP - Basics of PHPIntroduction to PHP - Basics of PHP
Introduction to PHP - Basics of PHP
 
Reverse Mapping (rmap) in Linux Kernel
Reverse Mapping (rmap) in Linux KernelReverse Mapping (rmap) in Linux Kernel
Reverse Mapping (rmap) in Linux Kernel
 
Process Address Space: The way to create virtual address (page table) of user...
Process Address Space: The way to create virtual address (page table) of user...Process Address Space: The way to create virtual address (page table) of user...
Process Address Space: The way to create virtual address (page table) of user...
 
Database constraints
Database constraintsDatabase constraints
Database constraints
 
COSCUP 2020 RISC-V 32 bit linux highmem porting
COSCUP 2020 RISC-V 32 bit linux highmem portingCOSCUP 2020 RISC-V 32 bit linux highmem porting
COSCUP 2020 RISC-V 32 bit linux highmem porting
 
Php famous built in functions
Php   famous built in functionsPhp   famous built in functions
Php famous built in functions
 
Dynamic memory allocation
Dynamic memory allocationDynamic memory allocation
Dynamic memory allocation
 
PHP Unit 3 functions_in_php_2
PHP Unit 3 functions_in_php_2PHP Unit 3 functions_in_php_2
PHP Unit 3 functions_in_php_2
 
Physical Memory Models.pdf
Physical Memory Models.pdfPhysical Memory Models.pdf
Physical Memory Models.pdf
 
Linux Kernel - Virtual File System
Linux Kernel - Virtual File SystemLinux Kernel - Virtual File System
Linux Kernel - Virtual File System
 
Decompressed vmlinux: linux kernel initialization from page table configurati...
Decompressed vmlinux: linux kernel initialization from page table configurati...Decompressed vmlinux: linux kernel initialization from page table configurati...
Decompressed vmlinux: linux kernel initialization from page table configurati...
 

Similar a PHP Data Objects

The History of PHPersistence
The History of PHPersistenceThe History of PHPersistence
The History of PHPersistenceHugo Hamon
 
Working with databases in Perl
Working with databases in PerlWorking with databases in Perl
Working with databases in PerlLaurent Dami
 
Introducing PHP Data Objects
Introducing PHP Data ObjectsIntroducing PHP Data Objects
Introducing PHP Data Objectswebhostingguy
 
Drupal - dbtng 25th Anniversary Edition
Drupal - dbtng 25th Anniversary EditionDrupal - dbtng 25th Anniversary Edition
Drupal - dbtng 25th Anniversary Editionddiers
 
Into to DBI with DBD::Oracle
Into to DBI with DBD::OracleInto to DBI with DBD::Oracle
Into to DBI with DBD::Oraclebyterock
 
Drupal II: The SQL
Drupal II: The SQLDrupal II: The SQL
Drupal II: The SQLddiers
 
Good Evils In Perl (Yapc Asia)
Good Evils In Perl (Yapc Asia)Good Evils In Perl (Yapc Asia)
Good Evils In Perl (Yapc Asia)Kang-min Liu
 
From mysql to MongoDB(MongoDB2011北京交流会)
From mysql to MongoDB(MongoDB2011北京交流会)From mysql to MongoDB(MongoDB2011北京交流会)
From mysql to MongoDB(MongoDB2011北京交流会)Night Sailer
 
PHP and Rich Internet Applications
PHP and Rich Internet ApplicationsPHP and Rich Internet Applications
PHP and Rich Internet Applicationselliando dias
 

Similar a PHP Data Objects (20)

Quebec pdo
Quebec pdoQuebec pdo
Quebec pdo
 
Quebec pdo
Quebec pdoQuebec pdo
Quebec pdo
 
The History of PHPersistence
The History of PHPersistenceThe History of PHPersistence
The History of PHPersistence
 
Working with databases in Perl
Working with databases in PerlWorking with databases in Perl
Working with databases in Perl
 
Sqlite perl
Sqlite perlSqlite perl
Sqlite perl
 
DBI
DBIDBI
DBI
 
veracruz
veracruzveracruz
veracruz
 
veracruz
veracruzveracruz
veracruz
 
veracruz
veracruzveracruz
veracruz
 
veracruz
veracruzveracruz
veracruz
 
Introducing PHP Data Objects
Introducing PHP Data ObjectsIntroducing PHP Data Objects
Introducing PHP Data Objects
 
Drupal - dbtng 25th Anniversary Edition
Drupal - dbtng 25th Anniversary EditionDrupal - dbtng 25th Anniversary Edition
Drupal - dbtng 25th Anniversary Edition
 
DataMapper
DataMapperDataMapper
DataMapper
 
Into to DBI with DBD::Oracle
Into to DBI with DBD::OracleInto to DBI with DBD::Oracle
Into to DBI with DBD::Oracle
 
Drupal II: The SQL
Drupal II: The SQLDrupal II: The SQL
Drupal II: The SQL
 
Good Evils In Perl (Yapc Asia)
Good Evils In Perl (Yapc Asia)Good Evils In Perl (Yapc Asia)
Good Evils In Perl (Yapc Asia)
 
Presentation1
Presentation1Presentation1
Presentation1
 
From mysql to MongoDB(MongoDB2011北京交流会)
From mysql to MongoDB(MongoDB2011北京交流会)From mysql to MongoDB(MongoDB2011北京交流会)
From mysql to MongoDB(MongoDB2011北京交流会)
 
PHP and Rich Internet Applications
PHP and Rich Internet ApplicationsPHP and Rich Internet Applications
PHP and Rich Internet Applications
 
Web 10 | PHP with MySQL
Web 10 | PHP with MySQLWeb 10 | PHP with MySQL
Web 10 | PHP with MySQL
 

Último

International Business Environments and Operations 16th Global Edition test b...
International Business Environments and Operations 16th Global Edition test b...International Business Environments and Operations 16th Global Edition test b...
International Business Environments and Operations 16th Global Edition test b...ssuserf63bd7
 
Independent Call Girls Andheri Nightlaila 9967584737
Independent Call Girls Andheri Nightlaila 9967584737Independent Call Girls Andheri Nightlaila 9967584737
Independent Call Girls Andheri Nightlaila 9967584737Riya Pathan
 
Call US-88OO1O2216 Call Girls In Mahipalpur Female Escort Service
Call US-88OO1O2216 Call Girls In Mahipalpur Female Escort ServiceCall US-88OO1O2216 Call Girls In Mahipalpur Female Escort Service
Call US-88OO1O2216 Call Girls In Mahipalpur Female Escort Servicecallgirls2057
 
8447779800, Low rate Call girls in Tughlakabad Delhi NCR
8447779800, Low rate Call girls in Tughlakabad Delhi NCR8447779800, Low rate Call girls in Tughlakabad Delhi NCR
8447779800, Low rate Call girls in Tughlakabad Delhi NCRashishs7044
 
Ten Organizational Design Models to align structure and operations to busines...
Ten Organizational Design Models to align structure and operations to busines...Ten Organizational Design Models to align structure and operations to busines...
Ten Organizational Design Models to align structure and operations to busines...Seta Wicaksana
 
(Best) ENJOY Call Girls in Faridabad Ex | 8377087607
(Best) ENJOY Call Girls in Faridabad Ex | 8377087607(Best) ENJOY Call Girls in Faridabad Ex | 8377087607
(Best) ENJOY Call Girls in Faridabad Ex | 8377087607dollysharma2066
 
Global Scenario On Sustainable and Resilient Coconut Industry by Dr. Jelfina...
Global Scenario On Sustainable  and Resilient Coconut Industry by Dr. Jelfina...Global Scenario On Sustainable  and Resilient Coconut Industry by Dr. Jelfina...
Global Scenario On Sustainable and Resilient Coconut Industry by Dr. Jelfina...ictsugar
 
Call Girls In Sikandarpur Gurgaon ❤️8860477959_Russian 100% Genuine Escorts I...
Call Girls In Sikandarpur Gurgaon ❤️8860477959_Russian 100% Genuine Escorts I...Call Girls In Sikandarpur Gurgaon ❤️8860477959_Russian 100% Genuine Escorts I...
Call Girls In Sikandarpur Gurgaon ❤️8860477959_Russian 100% Genuine Escorts I...lizamodels9
 
Buy gmail accounts.pdf Buy Old Gmail Accounts
Buy gmail accounts.pdf Buy Old Gmail AccountsBuy gmail accounts.pdf Buy Old Gmail Accounts
Buy gmail accounts.pdf Buy Old Gmail AccountsBuy Verified Accounts
 
Call Girls Miyapur 7001305949 all area service COD available Any Time
Call Girls Miyapur 7001305949 all area service COD available Any TimeCall Girls Miyapur 7001305949 all area service COD available Any Time
Call Girls Miyapur 7001305949 all area service COD available Any Timedelhimodelshub1
 
MAHA Global and IPR: Do Actions Speak Louder Than Words?
MAHA Global and IPR: Do Actions Speak Louder Than Words?MAHA Global and IPR: Do Actions Speak Louder Than Words?
MAHA Global and IPR: Do Actions Speak Louder Than Words?Olivia Kresic
 
BEST Call Girls In Old Faridabad ✨ 9773824855 ✨ Escorts Service In Delhi Ncr,
BEST Call Girls In Old Faridabad ✨ 9773824855 ✨ Escorts Service In Delhi Ncr,BEST Call Girls In Old Faridabad ✨ 9773824855 ✨ Escorts Service In Delhi Ncr,
BEST Call Girls In Old Faridabad ✨ 9773824855 ✨ Escorts Service In Delhi Ncr,noida100girls
 
Innovation Conference 5th March 2024.pdf
Innovation Conference 5th March 2024.pdfInnovation Conference 5th March 2024.pdf
Innovation Conference 5th March 2024.pdfrichard876048
 
The CMO Survey - Highlights and Insights Report - Spring 2024
The CMO Survey - Highlights and Insights Report - Spring 2024The CMO Survey - Highlights and Insights Report - Spring 2024
The CMO Survey - Highlights and Insights Report - Spring 2024christinemoorman
 
Market Sizes Sample Report - 2024 Edition
Market Sizes Sample Report - 2024 EditionMarket Sizes Sample Report - 2024 Edition
Market Sizes Sample Report - 2024 EditionMintel Group
 
Cybersecurity Awareness Training Presentation v2024.03
Cybersecurity Awareness Training Presentation v2024.03Cybersecurity Awareness Training Presentation v2024.03
Cybersecurity Awareness Training Presentation v2024.03DallasHaselhorst
 
Organizational Structure Running A Successful Business
Organizational Structure Running A Successful BusinessOrganizational Structure Running A Successful Business
Organizational Structure Running A Successful BusinessSeta Wicaksana
 
Digital Transformation in the PLM domain - distrib.pdf
Digital Transformation in the PLM domain - distrib.pdfDigital Transformation in the PLM domain - distrib.pdf
Digital Transformation in the PLM domain - distrib.pdfJos Voskuil
 
Marketplace and Quality Assurance Presentation - Vincent Chirchir
Marketplace and Quality Assurance Presentation - Vincent ChirchirMarketplace and Quality Assurance Presentation - Vincent Chirchir
Marketplace and Quality Assurance Presentation - Vincent Chirchirictsugar
 

Último (20)

International Business Environments and Operations 16th Global Edition test b...
International Business Environments and Operations 16th Global Edition test b...International Business Environments and Operations 16th Global Edition test b...
International Business Environments and Operations 16th Global Edition test b...
 
Independent Call Girls Andheri Nightlaila 9967584737
Independent Call Girls Andheri Nightlaila 9967584737Independent Call Girls Andheri Nightlaila 9967584737
Independent Call Girls Andheri Nightlaila 9967584737
 
Call US-88OO1O2216 Call Girls In Mahipalpur Female Escort Service
Call US-88OO1O2216 Call Girls In Mahipalpur Female Escort ServiceCall US-88OO1O2216 Call Girls In Mahipalpur Female Escort Service
Call US-88OO1O2216 Call Girls In Mahipalpur Female Escort Service
 
8447779800, Low rate Call girls in Tughlakabad Delhi NCR
8447779800, Low rate Call girls in Tughlakabad Delhi NCR8447779800, Low rate Call girls in Tughlakabad Delhi NCR
8447779800, Low rate Call girls in Tughlakabad Delhi NCR
 
Ten Organizational Design Models to align structure and operations to busines...
Ten Organizational Design Models to align structure and operations to busines...Ten Organizational Design Models to align structure and operations to busines...
Ten Organizational Design Models to align structure and operations to busines...
 
(Best) ENJOY Call Girls in Faridabad Ex | 8377087607
(Best) ENJOY Call Girls in Faridabad Ex | 8377087607(Best) ENJOY Call Girls in Faridabad Ex | 8377087607
(Best) ENJOY Call Girls in Faridabad Ex | 8377087607
 
Global Scenario On Sustainable and Resilient Coconut Industry by Dr. Jelfina...
Global Scenario On Sustainable  and Resilient Coconut Industry by Dr. Jelfina...Global Scenario On Sustainable  and Resilient Coconut Industry by Dr. Jelfina...
Global Scenario On Sustainable and Resilient Coconut Industry by Dr. Jelfina...
 
Call Girls In Sikandarpur Gurgaon ❤️8860477959_Russian 100% Genuine Escorts I...
Call Girls In Sikandarpur Gurgaon ❤️8860477959_Russian 100% Genuine Escorts I...Call Girls In Sikandarpur Gurgaon ❤️8860477959_Russian 100% Genuine Escorts I...
Call Girls In Sikandarpur Gurgaon ❤️8860477959_Russian 100% Genuine Escorts I...
 
Buy gmail accounts.pdf Buy Old Gmail Accounts
Buy gmail accounts.pdf Buy Old Gmail AccountsBuy gmail accounts.pdf Buy Old Gmail Accounts
Buy gmail accounts.pdf Buy Old Gmail Accounts
 
Call Girls Miyapur 7001305949 all area service COD available Any Time
Call Girls Miyapur 7001305949 all area service COD available Any TimeCall Girls Miyapur 7001305949 all area service COD available Any Time
Call Girls Miyapur 7001305949 all area service COD available Any Time
 
MAHA Global and IPR: Do Actions Speak Louder Than Words?
MAHA Global and IPR: Do Actions Speak Louder Than Words?MAHA Global and IPR: Do Actions Speak Louder Than Words?
MAHA Global and IPR: Do Actions Speak Louder Than Words?
 
BEST Call Girls In Old Faridabad ✨ 9773824855 ✨ Escorts Service In Delhi Ncr,
BEST Call Girls In Old Faridabad ✨ 9773824855 ✨ Escorts Service In Delhi Ncr,BEST Call Girls In Old Faridabad ✨ 9773824855 ✨ Escorts Service In Delhi Ncr,
BEST Call Girls In Old Faridabad ✨ 9773824855 ✨ Escorts Service In Delhi Ncr,
 
Innovation Conference 5th March 2024.pdf
Innovation Conference 5th March 2024.pdfInnovation Conference 5th March 2024.pdf
Innovation Conference 5th March 2024.pdf
 
The CMO Survey - Highlights and Insights Report - Spring 2024
The CMO Survey - Highlights and Insights Report - Spring 2024The CMO Survey - Highlights and Insights Report - Spring 2024
The CMO Survey - Highlights and Insights Report - Spring 2024
 
Corporate Profile 47Billion Information Technology
Corporate Profile 47Billion Information TechnologyCorporate Profile 47Billion Information Technology
Corporate Profile 47Billion Information Technology
 
Market Sizes Sample Report - 2024 Edition
Market Sizes Sample Report - 2024 EditionMarket Sizes Sample Report - 2024 Edition
Market Sizes Sample Report - 2024 Edition
 
Cybersecurity Awareness Training Presentation v2024.03
Cybersecurity Awareness Training Presentation v2024.03Cybersecurity Awareness Training Presentation v2024.03
Cybersecurity Awareness Training Presentation v2024.03
 
Organizational Structure Running A Successful Business
Organizational Structure Running A Successful BusinessOrganizational Structure Running A Successful Business
Organizational Structure Running A Successful Business
 
Digital Transformation in the PLM domain - distrib.pdf
Digital Transformation in the PLM domain - distrib.pdfDigital Transformation in the PLM domain - distrib.pdf
Digital Transformation in the PLM domain - distrib.pdf
 
Marketplace and Quality Assurance Presentation - Vincent Chirchir
Marketplace and Quality Assurance Presentation - Vincent ChirchirMarketplace and Quality Assurance Presentation - Vincent Chirchir
Marketplace and Quality Assurance Presentation - Vincent Chirchir
 

PHP Data Objects

  • 1. PHP Data Objects Wez Furlong <wez@messagesystems.com>
  • 2. About me • PHP Core Developer since 2001 • Author of the Streams layer • I hold the title “King” of PECL • Author of most of PDO and its drivers
  • 3. What is PDO? • PHP Data Objects • A set of PHP extensions that provide a core PDO class and database specific drivers • Focus on data access abstraction rather than database abstraction
  • 4. What can it do? • Prepare/execute, bound parameters • Transactions • LOBS • SQLSTATE standard error codes, flexible error handling • Portability attributes to smooth over database specific nuances
  • 5. What databases are supported? • MySQL, PostgreSQL • ODBC, DB2, OCI • SQLite • Sybase/FreeTDS/MSSQL
  • 6. Connecting try { $dbh = new PDO($dsn, $user, $password, $options); } catch (PDOException $e) { die(“Failed to connect:” . $e->getMessage(); }
  • 7. DSNs • mysql:host=name;dbname=dbname • pgsql:host=name dbname=dbname • odbc:odbc_dsn • oci:dbname=dbname;charset=charset • sqlite:/path/to/file
  • 8. Connection Management try { $dbh = new PDO($dsn, $user, $pw); // use the database here // ... // done; release $dbh = null; } catch (PDOException $e) { die($e->getMessage(); }
  • 9. DSN Aliasing • uri:uri • Specify location of a file that contains the actual DSN on the first line • Works with the streams interface, so remote URLs can work too (this has performance implications) • name (with no colon) • Maps to pdo.dsn.name in your php.ini • pdo.dsn.name=sqlite:/path/to/name.db
  • 10. DSN Aliasing pdo.dsn.name=sqlite:/path/to/name.db $dbh = new PDO(“name”); is equivalent to: $dbh = new PDO(“sqlite:path/to/name.db”);
  • 11. Persistent Connections // Connection stays alive between requests $dbh = new PDO($dsn, $user, $pass, array( PDO::ATTR_PERSISTENT => true ) );
  • 12. Persistent Connections // Specify your own cache key $dbh = new PDO($dsn, $user, $pass, array( PDO::ATTR_PERSISTENT => “my-key” ) ); Useful for keeping separate persistent connections
  • 13. 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 (requires that your web server be restarted)
  • 14. Error Handling • Maps error codes to ANSI SQLSTATE (5 character text string) • also provides the native db error information • Three error handling strategies • silent (default) • warning • exception
  • 15. PDO::ERRMODE_SILENT // The default mode if (!dbh->query($sql)) { echo $dbh->errorCode(), “<br>”; $info = $dbh->errorInfo(); // $info[0] == $dbh->errorCode() // SQLSTATE error code // $info[1] is driver specific err code // $info[2] is driver specific // error message }
  • 16. PDO::ERRMODE_WARNING $dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_WARNING); Behaves the same as silent mode Raises an E_WARNING as errors are detected Can suppress with @ operator as usual
  • 17. PDO::ERRMODE_EXCEPTION $dbh->setAttribute(PDO::ATTR_ERRMODE, 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] driver specific error code // $info[2] driver specific error string }
  • 18. Get data $dbh = new PDO($dsn); $stmt = $dbh->prepare( “SELECT * FROM FOO”); $stmt->execute(); while ($row = $stmt->fetch()) { print_r($row); } $stmt = null;
  • 19. Forward-only cursors • a.k.a. “unbuffered” queries in mysql parlance • They are the default cursor type • rowCount() doesn’t have meaning • FAST!
  • 20. Forward-only cursors • Other queries are likely to block • You must fetch all remaining data before launching another query • $stmt->closeCursor();
  • 21. 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); }
  • 22. Data typing • Very loose • Prefers strings • Gives you more control over data conversion
  • 23. Fetch modes • $stmt->fetch(PDO::FETCH_BOTH); - Array with numeric and string keys - default option • PDO::FETCH_NUM - numeric keys only • PDO::FETCH_ASSOC - string keys only
  • 24. 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
  • 25. Fetch modes • PDO::FETCH_COLUMN - Fetches a column (example later) • PDO::FETCH_BOUND - Only fetches into bound variables • PDO::FETCH_FUNC - Returns the result filtered through a callback • see the manual for more
  • 26. Iterators $dbh = new PDO($dsn); $stmt = $dbh->query( “SELECT name FROM FOO”, PDO::FETCH_COLUMN, 0); foreach ($stmt as $name) { echo “Name: $namen”; } $stmt = null;
  • 27. Changing data $deleted = $dbh->exec( “DELETE FROM FOO WHERE 1”); $changes = $dbh->exec( “UPDATE FOO SET active=1 ” .“WHERE NAME LIKE ‘%joe%’”);
  • 28. Autonumber/sequences $dbh->exec( “insert into foo values (...)”); echo $dbh->lastInsertId(); $dbh->exec( “insert into foo values (...)”); echo $dbh->lastInsertId(“seqname”); Its up to you to call the right one for your db!
  • 29. Prepared Statements // No need to manually quote data here $stmt = $dbh->prepare( “INSERT INTO CREDITS (extension, name)” .“VALUES (:extension, :name)”); $stmt->execute(array( ‘extension’ => ‘xdebug’, ‘name’ => ‘Derick Rethans’ ));
  • 30. Prepared Statements // No need to manually quote data here $stmt = $dbh->prepare( “INSERT INTO CREDITS (extension, name)” .“VALUES (?, ?)”); $stmt->execute(array( ‘xdebug’, ‘Derick Rethans’ ));
  • 31. $db->quote() • If you really must quote things “by-hand” • $db->quote() adds quotes and proper escaping as needed • But doesn’t do anything in the ODBC driver! • Best to use prepared statements
  • 32. Transactions $dbh->beginTransaction(); try { $dbh->query(“UPDATE ...”); $dbh->query(“UPDATE ...”); $dbh->commit(); } catch (PDOException $e) { $dbh->rollBack(); }
  • 33. Stored Procedures $stmt = $dbh->prepare( “CALL sp_set_string(?)”); $stmt->execute(array(‘foo’)); $stmt = $dbh->prepare( “CALL sp_set_string(?)”); $stmt->bindValue(1, ‘foo’); $stmt->execute();
  • 34. OUT parameters $stmt = $dbh->prepare( “CALL sp_get_string(?)”); $stmt->bindParam(1, $ret, PDO::PARAM_STR, 4000); if ($stmt->execute()) { echo “Got $retn”; }
  • 35. IN/OUT parameters $stmt = $dbh->prepare( “call @sp_inout(?)”); $val = “My input data”; $stmt->bindParam(1, $val, PDO::PARAM_STR| PDO::PARAM_INPUT_OUTPUT, 4000); if ($stmt->execute()) { echo “Got $valn”; }
  • 36. Multi-rowset queries $stmt = $dbh->query( “call sp_multi_results()”); do { while ($row = $stmt->fetch()) { print_r($row); } } while ($stmt->nextRowset());
  • 37. Binding columns $stmt = $dbh->prepare( “SELECT extension, name from CREDITS”); if ($stmt->execute()) { $stmt->bindColumn(‘extension’, $ext); $stmt->bindColumn(‘name’, $name); while ($stmt->fetch(PDO::FETCH_BOUND)) { echo “Extension: $extn”; echo “Author: $namen”; } }
  • 38. Portability Aids • PDO aims to make it easier to write db independent apps • A number of hacks^Wtweaks for this purpose
  • 39. 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
  • 40. 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);
  • 41. LOBs • Large objects are usually >4kb in size • Nice to avoid fetching them until you need them • Mature RDBMS offer LOB APIs for this • PDO exposes LOBs as Streams
  • 42. Fetching an image $stmt = $dbh->prepare( “select contenttype, imagedata” .“ from images where id=?”); $stmt->execute(array($_GET[‘id’])); $stmt->bindColumn(1, $type, PDO::PARAM_STR, 256); $stmt->bindColumn(2, $lob, PDO::PARAM_LOB); $stmt->fetch(PDO::FETCH_BOUND); header(“Content-Type: $type”); fpassthru($lob);
  • 43. Uploading an image $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();
  • 44. Scrollable Cursors • Allow random access to a rowset • Higher resource usage than forward-only cursors • Can be used for positioned updates (more useful for CLI/GUI apps)
  • 45. 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
  • 46. Questions? • Find these slides on my blog and on slideshare.net • My blog: http://netevil.org/ • Gold: http://troels.arvin.dk/db/rdbms/#select-limit-offset