Mr. Ajinkya NaharMr. Ajinkya Nahar
Twitter - @ajinkyanaharTwitter - @ajinkyanahar
LinkedIn –LinkedIn –
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
The PDOStatement object represents a prepared
statement, as well as a result set from an executed SQL
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
After that, we can use its methods to access the database.
Execute an SQL statement returning the number of rows
Execute an SQL statement returning a result set as a
Prepares a statement returning a result set as a
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),
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
For a result set:
fetch() :-Returns the next row.
Useful arguments: PDO::FETCH_ASSOC,
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";
$pdo = new PDO ($dsn, $user, $password);
die ('Oops'); // Exit, displaying an error message
Establish Connection to MySql
For SELECT query -
$sql = 'SELECT * FROM users WHERE email = ? AND
password = ?';
$pds = $pdo->prepare($sql);