2
Most read
3
Most read
5
Most read
PHP Data Objects
(PDO)
Introduction
 PHP5 extension written in a compiled language (C/C++).
 Provides data-access abstraction layer.
 Lightweight, consistent interface.
 Play a major role between PHP servers and databases.
 Work as connectors between PHP site and its supporting
database.
 Supports any database PDO driver has been written for.
DataBase Supports
 CUBRID
 Firebird
 IBM
 Informix
 Oracle
 MySQL
 PostgreSQL
 Ms SQL Server
 SQLite
 ODBC & DB2
How to Identify ?
Another Way..
<?php
print_r(PDO::getAvailableDrivers());
Output:
Array ( [0] => mysql )
How to Connect
<?php
$handler = new PDO('mysql:host=localhost;dbname=pdotest', 'username','password');
Database Type HostName
DataBase Name
DB UserName
DB Password
Fetch Data
➔ Associative Array
➔ Numeric Array
➔ Std Class Object
➔ Custom Class object
Examples
Database Name - pdotest
Table Name - person
Fetch Array
<?php
$host = 'localhost';
$user = 'root';
$password = 'root';
$database = 'pdotest';
$handler = new PDO('mysql:host=' . $host . ';dbname=' . $database . '', $user,
$password);
$query = $handler->query('SELECT * FROM person');
echo '<pre>', print_r($query->fetch()), '</pre>';
Output:
Array
(
[id] => 2
[0] => 2
[firstName] => Prashant
[1] => Prashant
[lastName] => Marathe
[2] => Marathe
[message] => This is Test Message
[3] => This is Test Message
[timeCreated] => 2015-02-21 15:14:13
[4] => 2015-02-21 15:14:13
)
Fetch All Data
<?php
$query = $handler->query('SELECT * FROM person');
$query->setFetchMode(PDO::FETCH_ASSOC);
echo '<pre>', print_r($query->fetchAll()), '</pre>';
Array
(
[0] => Array
(
[id] => 2
[firstName] => Prashant
[lastName] => Marathe
[message] => This is Test Message
[timeCreated] => 2015-02-21 15:14:13
)
[1] => Array
(
[id] => 3
[firstName] => Atul
[lastName] => Joshi
[message] => This is Test Message
[timeCreated] => 2015-02-21 15:16:02
)
Here you can also set
the fetching mode
Fetch Object
<?php
$query = $handler->query('SELECT * FROM person');
$query->setFetchMode(PDO::FETCH_OBJ);
echo '<pre>', print_r($query->fetch()), '</pre>';
Output:
stdClass Object
(
[id] => 2
[firstName] => Prashant
[lastName] => Marathe
[message] => This is Test Message
[timeCreated] => 2015-02-21 15:14:13
)
This returns an object of stdclass
Fetch Data In Class
<?php
class person {
public $id, $firstName, $lastName, $message, $timeCreated, $newProperty;
public function __construct() {
$this->newProperty = 'This is new property';
}
}
$query = $handler->query('SELECT * FROM person');
$query->setFetchMode(PDO::FETCH_CLASS, 'person');
echo '<pre>', print_r($query->fetch()), '</pre>';
Output:
person Object
(
[id] => 2
[firstName] => Prashant
[lastName] => Marathe
[message] => This is Test Message
[timeCreated] => 2015-02-21 15:14:13
[newProperty] => This is new property
)
Prepared Statements
➢ Precompiled SQL statements that can be
executed multiple times by sending just data to
the server
➢ Prepared statements will help us to protect from
SQL injection
Use Placeholders
<?php
$firstName = "Test";$lastName = "User";$message = "This is test message";
$timeCreated = date('Y-m-d H:i:s');
// Prepare statement to make a execution
$query = $handler->prepare('INSERT INTO person (firstName, lastName, message,
timeCreated) values(?, ?, ?, ?)');
// Bind the params
$query->bindParam(1, $firstName);
$query->bindParam(2, $lastName);
$query->bindParam(3, $message);
$query->bindParam(4, $timeCreated);
$query->execute();
Unnamed Placeholders
Named Placeholders
<?php
// Prepare statement to make a execution
$query = $handler->prepare('INSERT INTO person (firstName, lastName, message,
timeCreated) values(:firstName, :lastName, :message, :timeCreated)');
// Bind the params
$query->bindParam(':firstName', $firstName);
$query->bindParam(':lastName', $lastName);
$query->bindParam(':message', $message);
$query->bindParam(':timeCreated', $timeCreated);
$firstName = "Test";$lastName = "User";$message = "This is test message";
$timeCreated = date('Y-m-d H:i:s');
$query->execute();
Output
Where it is in TYPO3Flow ?
Settings.yaml Directory Structure
Driver.php
PDOConnection.php
The points which we covered
➢ What is PDO ?
➢ Supported databases.
➢ How to identify available or installed PDO
driver's ?
➢ How to connect with PDO ?
➢ How to fetch the data?
➢ Prepared Statements
➢ How it is used in TYPO3 Flow?
Advantages
✔ Support great number of database systems
supported by PHP
✔ Better Security
✔ Improved performance
✔ Enhanced reliability
Disadvantages
✗ PDO only offers Object Oriented API
Thank You !

More Related Content

PDF
PHP Data Objects
PPT
Quebec pdo
PPT
PHP - PDO Objects
KEY
Php 101: PDO
PDF
Quebec pdo
PPTX
Pdo – php database extension-Phpgurukul
ODP
Adodb Pdo Presentation
PPT
Zend framework 03 - singleton factory data mapper caching logging
PHP Data Objects
Quebec pdo
PHP - PDO Objects
Php 101: PDO
Quebec pdo
Pdo – php database extension-Phpgurukul
Adodb Pdo Presentation
Zend framework 03 - singleton factory data mapper caching logging

What's hot (18)

PDF
PDO Basics - PHPMelb 2014
PPT
Introducing PHP Data Objects
PDF
Dependency Injection with PHP and PHP 5.3
PDF
Doctrine 2
PDF
Dependency Injection in Laravel
PDF
jQuery secrets
PDF
Sqlite perl
PPT
Corephpcomponentpresentation 1211425966721657-8
PDF
Php unit the-mostunknownparts
PDF
Http and security
PDF
Migrating to dependency injection
PDF
HeadCouch - CouchDB PHP Client
PPTX
Swing database(mysql)
PDF
All Things Open 2016 -- Database Programming for Newbies
PDF
Php unit the-mostunknownparts
PDF
international PHP2011_Bastian Feder_jQuery's Secrets
PDF
veracruz
PDO Basics - PHPMelb 2014
Introducing PHP Data Objects
Dependency Injection with PHP and PHP 5.3
Doctrine 2
Dependency Injection in Laravel
jQuery secrets
Sqlite perl
Corephpcomponentpresentation 1211425966721657-8
Php unit the-mostunknownparts
Http and security
Migrating to dependency injection
HeadCouch - CouchDB PHP Client
Swing database(mysql)
All Things Open 2016 -- Database Programming for Newbies
Php unit the-mostunknownparts
international PHP2011_Bastian Feder_jQuery's Secrets
veracruz
Ad

Similar to PHP Data Objects (20)

PDF
Advanced Php - Macq Electronique 2010
PDF
veracruz
PDF
veracruz
PDF
veracruz
PPTX
12-OO-PHP.pptx
KEY
PPT
Intro to php
PDF
Service discovery and configuration provisioning
PDF
How to Create Login and Registration API in PHP.pdf
PDF
From mysql to MongoDB(MongoDB2011北京交流会)
PDF
Doctrine and NoSQL
PPT
Part 2
PPTX
Lecture9_OOPHP_SPring2023.pptx
PPTX
Drupal 8 migrate!
PDF
Doctrine for NoSQL
PDF
Meet Magento Sweden - Magento 2 Layout and Code Compilation for Performance
PDF
Php summary
PPT
Working with databases in Perl
PDF
Spot the Web Vulnerability
PPTX
[PHP] Zend_Db (Zend Framework)
Advanced Php - Macq Electronique 2010
veracruz
veracruz
veracruz
12-OO-PHP.pptx
Intro to php
Service discovery and configuration provisioning
How to Create Login and Registration API in PHP.pdf
From mysql to MongoDB(MongoDB2011北京交流会)
Doctrine and NoSQL
Part 2
Lecture9_OOPHP_SPring2023.pptx
Drupal 8 migrate!
Doctrine for NoSQL
Meet Magento Sweden - Magento 2 Layout and Code Compilation for Performance
Php summary
Working with databases in Perl
Spot the Web Vulnerability
[PHP] Zend_Db (Zend Framework)
Ad

Recently uploaded (20)

PDF
Video forgery: An extensive analysis of inter-and intra-frame manipulation al...
PDF
Taming the Chaos: How to Turn Unstructured Data into Decisions
PDF
Zenith AI: Advanced Artificial Intelligence
PPTX
Web Crawler for Trend Tracking Gen Z Insights.pptx
PDF
A comparative study of natural language inference in Swahili using monolingua...
PDF
Assigned Numbers - 2025 - Bluetooth® Document
PDF
STKI Israel Market Study 2025 version august
PDF
Hindi spoken digit analysis for native and non-native speakers
PDF
TrustArc Webinar - Click, Consent, Trust: Winning the Privacy Game
PDF
CloudStack 4.21: First Look Webinar slides
PDF
WOOl fibre morphology and structure.pdf for textiles
PDF
August Patch Tuesday
PPTX
Group 1 Presentation -Planning and Decision Making .pptx
PPTX
Modernising the Digital Integration Hub
PDF
Getting Started with Data Integration: FME Form 101
PPT
Module 1.ppt Iot fundamentals and Architecture
PDF
sustainability-14-14877-v2.pddhzftheheeeee
DOCX
search engine optimization ppt fir known well about this
PPTX
Chapter 5: Probability Theory and Statistics
PDF
How ambidextrous entrepreneurial leaders react to the artificial intelligence...
Video forgery: An extensive analysis of inter-and intra-frame manipulation al...
Taming the Chaos: How to Turn Unstructured Data into Decisions
Zenith AI: Advanced Artificial Intelligence
Web Crawler for Trend Tracking Gen Z Insights.pptx
A comparative study of natural language inference in Swahili using monolingua...
Assigned Numbers - 2025 - Bluetooth® Document
STKI Israel Market Study 2025 version august
Hindi spoken digit analysis for native and non-native speakers
TrustArc Webinar - Click, Consent, Trust: Winning the Privacy Game
CloudStack 4.21: First Look Webinar slides
WOOl fibre morphology and structure.pdf for textiles
August Patch Tuesday
Group 1 Presentation -Planning and Decision Making .pptx
Modernising the Digital Integration Hub
Getting Started with Data Integration: FME Form 101
Module 1.ppt Iot fundamentals and Architecture
sustainability-14-14877-v2.pddhzftheheeeee
search engine optimization ppt fir known well about this
Chapter 5: Probability Theory and Statistics
How ambidextrous entrepreneurial leaders react to the artificial intelligence...

PHP Data Objects

  • 2. Introduction  PHP5 extension written in a compiled language (C/C++).  Provides data-access abstraction layer.  Lightweight, consistent interface.  Play a major role between PHP servers and databases.  Work as connectors between PHP site and its supporting database.  Supports any database PDO driver has been written for.
  • 3. DataBase Supports  CUBRID  Firebird  IBM  Informix  Oracle  MySQL  PostgreSQL  Ms SQL Server  SQLite  ODBC & DB2
  • 6. How to Connect <?php $handler = new PDO('mysql:host=localhost;dbname=pdotest', 'username','password'); Database Type HostName DataBase Name DB UserName DB Password
  • 7. Fetch Data ➔ Associative Array ➔ Numeric Array ➔ Std Class Object ➔ Custom Class object
  • 8. Examples Database Name - pdotest Table Name - person
  • 9. Fetch Array <?php $host = 'localhost'; $user = 'root'; $password = 'root'; $database = 'pdotest'; $handler = new PDO('mysql:host=' . $host . ';dbname=' . $database . '', $user, $password); $query = $handler->query('SELECT * FROM person'); echo '<pre>', print_r($query->fetch()), '</pre>'; Output: Array ( [id] => 2 [0] => 2 [firstName] => Prashant [1] => Prashant [lastName] => Marathe [2] => Marathe [message] => This is Test Message [3] => This is Test Message [timeCreated] => 2015-02-21 15:14:13 [4] => 2015-02-21 15:14:13 )
  • 10. Fetch All Data <?php $query = $handler->query('SELECT * FROM person'); $query->setFetchMode(PDO::FETCH_ASSOC); echo '<pre>', print_r($query->fetchAll()), '</pre>'; Array ( [0] => Array ( [id] => 2 [firstName] => Prashant [lastName] => Marathe [message] => This is Test Message [timeCreated] => 2015-02-21 15:14:13 ) [1] => Array ( [id] => 3 [firstName] => Atul [lastName] => Joshi [message] => This is Test Message [timeCreated] => 2015-02-21 15:16:02 ) Here you can also set the fetching mode
  • 11. Fetch Object <?php $query = $handler->query('SELECT * FROM person'); $query->setFetchMode(PDO::FETCH_OBJ); echo '<pre>', print_r($query->fetch()), '</pre>'; Output: stdClass Object ( [id] => 2 [firstName] => Prashant [lastName] => Marathe [message] => This is Test Message [timeCreated] => 2015-02-21 15:14:13 ) This returns an object of stdclass
  • 12. Fetch Data In Class <?php class person { public $id, $firstName, $lastName, $message, $timeCreated, $newProperty; public function __construct() { $this->newProperty = 'This is new property'; } } $query = $handler->query('SELECT * FROM person'); $query->setFetchMode(PDO::FETCH_CLASS, 'person'); echo '<pre>', print_r($query->fetch()), '</pre>'; Output: person Object ( [id] => 2 [firstName] => Prashant [lastName] => Marathe [message] => This is Test Message [timeCreated] => 2015-02-21 15:14:13 [newProperty] => This is new property )
  • 13. Prepared Statements ➢ Precompiled SQL statements that can be executed multiple times by sending just data to the server ➢ Prepared statements will help us to protect from SQL injection
  • 14. Use Placeholders <?php $firstName = "Test";$lastName = "User";$message = "This is test message"; $timeCreated = date('Y-m-d H:i:s'); // Prepare statement to make a execution $query = $handler->prepare('INSERT INTO person (firstName, lastName, message, timeCreated) values(?, ?, ?, ?)'); // Bind the params $query->bindParam(1, $firstName); $query->bindParam(2, $lastName); $query->bindParam(3, $message); $query->bindParam(4, $timeCreated); $query->execute(); Unnamed Placeholders
  • 15. Named Placeholders <?php // Prepare statement to make a execution $query = $handler->prepare('INSERT INTO person (firstName, lastName, message, timeCreated) values(:firstName, :lastName, :message, :timeCreated)'); // Bind the params $query->bindParam(':firstName', $firstName); $query->bindParam(':lastName', $lastName); $query->bindParam(':message', $message); $query->bindParam(':timeCreated', $timeCreated); $firstName = "Test";$lastName = "User";$message = "This is test message"; $timeCreated = date('Y-m-d H:i:s'); $query->execute();
  • 17. Where it is in TYPO3Flow ? Settings.yaml Directory Structure
  • 20. The points which we covered ➢ What is PDO ? ➢ Supported databases. ➢ How to identify available or installed PDO driver's ? ➢ How to connect with PDO ? ➢ How to fetch the data? ➢ Prepared Statements ➢ How it is used in TYPO3 Flow?
  • 21. Advantages ✔ Support great number of database systems supported by PHP ✔ Better Security ✔ Improved performance ✔ Enhanced reliability
  • 22. Disadvantages ✗ PDO only offers Object Oriented API