SlideShare a Scribd company logo
2
Most read
5
Most read
Presented by:-
Mr. Ajinkya NaharMr. Ajinkya Nahar
Twitter - @ajinkyanaharTwitter - @ajinkyanahar
LinkedIn –LinkedIn –
http://in.linkedin.com/pub/ajinkya-http://in.linkedin.com/pub/ajinkya-
nahar/8/404/77bnahar/8/404/77b
Features
PDO requires PHP Version 5.
PDO makes use of two main objects.
Once we have established a connection to the specific
database, the methods used to access and manipulate data
are all generic, and do not require re-writing if you change
the type of database.
The PDO object itself represents a connection to the
database, and provides simple methods to execute an SQL
statement.
The PDOStatement object represents a prepared
statement, as well as a result set from an executed SQL
statement.
PDO Object and most useful statement
This represents a connection to the Database.
All database operations are initiated through the PDO
object. The PDO object is created when you connect to the
database.
After that, we can use its methods to access the database.
exec():-
Execute an SQL statement returning the number of rows
affected.
query() :-
Execute an SQL statement returning a result set as a
PDOStatement .
prepare():-
Prepares a statement returning a result set as a
PDOStatement.
We can use question marks (?) for values.
we can then call the execute(array()) method.
The PDOStatement represents a prepared statement, as
well as a returned result set.
PDO->query operation (where it represents a result set),
a
PDO->prepare operation (where it represents a
prepared statement) or a
PDO->execute operation (where it represents a result
set from your prepared statement)
For a prepared statement:-
execute() :-Execute the prepared statement. We can use
an array of values to replace the question mark
parameters.
Prepared Statement
For a result set:
fetch() :-Returns the next row.
Useful arguments: PDO::FETCH_ASSOC,
PDO::FETCH_NUM,
PDO::FETCH_BOTH (default)
fetchAll():- Returns the whole result set as an array
fetchColumn() :-Returns a single column of the next row.
$database = ‘database_name';
$user = ‘user';
$password = ‘password';
$dsn = "mysql:host=localhost;dbname=$database";
try {
$pdo = new PDO ($dsn, $user, $password);
}
catch(PDOException $e)
{
die ('Oops'); // Exit, displaying an error message
}
Establish Connection to MySql
Code Examples
For SELECT query -
$sql = 'SELECT * FROM users WHERE email = ? AND
password = ?';
$pds = $pdo->prepare($sql);
$pds->execute(array(
$email,
$password
));
For INSERT query -
$sql = 'INSERT INTO users(email,password)
VALUES(?,?)';
$pds = $pdo->prepare($sql);
$pds->execute(array(
$email,
$password
));
For UPDATE query -
$sql = 'UPDATE users SET email=?, password=?
WHERE id=?';
$pds = $pdo->prepare($sql);
$pds->execute(array(
$email,
$password,
$id
));
For DELETE query -
$sql = 'DELETE FROM users WHERE id = ?';
$pds = $pdo->prepare($sql);
$pds->execute(array(
$id
));
For like query -
$sql = 'SELECT * FROM users WHERE email like ?';
$pds = $pdo->prepare($sql);
$pds->execute(array(
‘%$email%’
));
PHP - PDO Objects

More Related Content

PPTX
Object oriented programming in python
PPTX
Introduction to java
PPTX
Files and streams In Java
PDF
Oops concepts || Object Oriented Programming Concepts in Java
PPTX
Web development with django - Basics Presentation
PPTX
Threads in Java
PPTX
Form Handling using PHP
PDF
JavaScript - Chapter 8 - Objects
Object oriented programming in python
Introduction to java
Files and streams In Java
Oops concepts || Object Oriented Programming Concepts in Java
Web development with django - Basics Presentation
Threads in Java
Form Handling using PHP
JavaScript - Chapter 8 - Objects

What's hot (20)

PDF
PPT
JAVA OOP
PPSX
Fondamentaux java
PPTX
Packages,static,this keyword in java
PPT
Method overriding
PPTX
Python Functions
PPT
Testing In Django
PDF
Inheritance In Java
PDF
Introduction to django framework
PDF
A Basic Django Introduction
PPTX
Introduction to java
PPT
Control Structures In Php 2
PDF
OOP Assignment 03.pdf
PPT
Most Asked Java Interview Question and Answer
PPT
Core java concepts
PPTX
Core java complete ppt(note)
PPTX
Android activity lifecycle
PPTX
Introduction to JAVA
PPTX
Regular expressions in Python
ODP
Python Modules
JAVA OOP
Fondamentaux java
Packages,static,this keyword in java
Method overriding
Python Functions
Testing In Django
Inheritance In Java
Introduction to django framework
A Basic Django Introduction
Introduction to java
Control Structures In Php 2
OOP Assignment 03.pdf
Most Asked Java Interview Question and Answer
Core java concepts
Core java complete ppt(note)
Android activity lifecycle
Introduction to JAVA
Regular expressions in Python
Python Modules
Ad

Similar to PHP - PDO Objects (20)

PDF
ZendCon2010 The Doctrine Project
PDF
9 Python programming notes for ktu physics and computer application semester 4
PPTX
U4-01-Node JS.pptxweasrdtfyhg[]"Piuytrhedfyguhijokpl
PDF
Quebec pdo
PPTX
Symfony2 Introduction Presentation
PPT
Advanced PHPUnit Testing
PPTX
3.java database connectivity
ODP
Exploring Symfony's Code
PPTX
Adding a modern twist to legacy web applications
PPT
JDBC Connecticity.ppt
PPTX
Local SQLite Database with Node for beginners
PPT
Php MySql For Beginners
PPT
Php and MySQL Web Development
PPT
Php Data Objects
PDF
Patterns and Tools for Database Versioning, Migration, Data Loading and Test ...
PDF
java4th.pdf bilgisayar mühendisliği bölümü
PPTX
working with PHP & DB's
PDF
How To Use IO Monads in Scala?
ZendCon2010 The Doctrine Project
9 Python programming notes for ktu physics and computer application semester 4
U4-01-Node JS.pptxweasrdtfyhg[]"Piuytrhedfyguhijokpl
Quebec pdo
Symfony2 Introduction Presentation
Advanced PHPUnit Testing
3.java database connectivity
Exploring Symfony's Code
Adding a modern twist to legacy web applications
JDBC Connecticity.ppt
Local SQLite Database with Node for beginners
Php MySql For Beginners
Php and MySQL Web Development
Php Data Objects
Patterns and Tools for Database Versioning, Migration, Data Loading and Test ...
java4th.pdf bilgisayar mühendisliği bölümü
working with PHP & DB's
How To Use IO Monads in Scala?
Ad

Recently uploaded (20)

PPTX
Effective Security Operations Center (SOC) A Modern, Strategic, and Threat-In...
PPTX
KOM of Painting work and Equipment Insulation REV00 update 25-dec.pptx
PDF
cuic standard and advanced reporting.pdf
PDF
Per capita expenditure prediction using model stacking based on satellite ima...
PDF
Bridging biosciences and deep learning for revolutionary discoveries: a compr...
PDF
Modernizing your data center with Dell and AMD
PPTX
Understanding_Digital_Forensics_Presentation.pptx
PDF
Build a system with the filesystem maintained by OSTree @ COSCUP 2025
PDF
How UI/UX Design Impacts User Retention in Mobile Apps.pdf
PPTX
Cloud computing and distributed systems.
PDF
Review of recent advances in non-invasive hemoglobin estimation
PDF
Reach Out and Touch Someone: Haptics and Empathic Computing
PPTX
20250228 LYD VKU AI Blended-Learning.pptx
PDF
Architecting across the Boundaries of two Complex Domains - Healthcare & Tech...
PPT
“AI and Expert System Decision Support & Business Intelligence Systems”
PDF
TokAI - TikTok AI Agent : The First AI Application That Analyzes 10,000+ Vira...
DOCX
The AUB Centre for AI in Media Proposal.docx
PDF
Encapsulation_ Review paper, used for researhc scholars
PDF
Electronic commerce courselecture one. Pdf
PPTX
Big Data Technologies - Introduction.pptx
Effective Security Operations Center (SOC) A Modern, Strategic, and Threat-In...
KOM of Painting work and Equipment Insulation REV00 update 25-dec.pptx
cuic standard and advanced reporting.pdf
Per capita expenditure prediction using model stacking based on satellite ima...
Bridging biosciences and deep learning for revolutionary discoveries: a compr...
Modernizing your data center with Dell and AMD
Understanding_Digital_Forensics_Presentation.pptx
Build a system with the filesystem maintained by OSTree @ COSCUP 2025
How UI/UX Design Impacts User Retention in Mobile Apps.pdf
Cloud computing and distributed systems.
Review of recent advances in non-invasive hemoglobin estimation
Reach Out and Touch Someone: Haptics and Empathic Computing
20250228 LYD VKU AI Blended-Learning.pptx
Architecting across the Boundaries of two Complex Domains - Healthcare & Tech...
“AI and Expert System Decision Support & Business Intelligence Systems”
TokAI - TikTok AI Agent : The First AI Application That Analyzes 10,000+ Vira...
The AUB Centre for AI in Media Proposal.docx
Encapsulation_ Review paper, used for researhc scholars
Electronic commerce courselecture one. Pdf
Big Data Technologies - Introduction.pptx

PHP - PDO Objects

  • 1. Presented by:- Mr. Ajinkya NaharMr. Ajinkya Nahar Twitter - @ajinkyanaharTwitter - @ajinkyanahar LinkedIn –LinkedIn – http://in.linkedin.com/pub/ajinkya-http://in.linkedin.com/pub/ajinkya- nahar/8/404/77bnahar/8/404/77b
  • 2. Features PDO requires PHP Version 5. PDO makes use of two main objects. Once we have established a connection to the specific database, the methods used to access and manipulate data are all generic, and do not require re-writing if you change the type of database. The PDO object itself represents a connection to the database, and provides simple methods to execute an SQL statement. The PDOStatement object represents a prepared statement, as well as a result set from an executed SQL statement.
  • 3. PDO Object and most useful statement This represents a connection to the Database. All database operations are initiated through the PDO object. The PDO object is created when you connect to the database. After that, we can use its methods to access the database. exec():- Execute an SQL statement returning the number of rows affected. query() :- Execute an SQL statement returning a result set as a PDOStatement .
  • 4. prepare():- Prepares a statement returning a result set as a PDOStatement. We can use question marks (?) for values. we can then call the execute(array()) method.
  • 5. The PDOStatement represents a prepared statement, as well as a returned result set. PDO->query operation (where it represents a result set), a PDO->prepare operation (where it represents a prepared statement) or a PDO->execute operation (where it represents a result set from your prepared statement) For a prepared statement:- execute() :-Execute the prepared statement. We can use an array of values to replace the question mark parameters. Prepared Statement
  • 6. For a result set: fetch() :-Returns the next row. Useful arguments: PDO::FETCH_ASSOC, PDO::FETCH_NUM, PDO::FETCH_BOTH (default) fetchAll():- Returns the whole result set as an array fetchColumn() :-Returns a single column of the next row.
  • 7. $database = ‘database_name'; $user = ‘user'; $password = ‘password'; $dsn = "mysql:host=localhost;dbname=$database"; try { $pdo = new PDO ($dsn, $user, $password); } catch(PDOException $e) { die ('Oops'); // Exit, displaying an error message } Establish Connection to MySql
  • 8. Code Examples For SELECT query - $sql = 'SELECT * FROM users WHERE email = ? AND password = ?'; $pds = $pdo->prepare($sql); $pds->execute(array( $email, $password ));
  • 9. For INSERT query - $sql = 'INSERT INTO users(email,password) VALUES(?,?)'; $pds = $pdo->prepare($sql); $pds->execute(array( $email, $password ));
  • 10. For UPDATE query - $sql = 'UPDATE users SET email=?, password=? WHERE id=?'; $pds = $pdo->prepare($sql); $pds->execute(array( $email, $password, $id ));
  • 11. For DELETE query - $sql = 'DELETE FROM users WHERE id = ?'; $pds = $pdo->prepare($sql); $pds->execute(array( $id ));
  • 12. For like query - $sql = 'SELECT * FROM users WHERE email like ?'; $pds = $pdo->prepare($sql); $pds->execute(array( ‘%$email%’ ));