php - pdo objects
DESCRIPTION
PHP - Mysql using PDO extentionTRANSCRIPT
![Page 1: PHP - PDO Objects](https://reader034.vdocuments.site/reader034/viewer/2022050805/559867e51a28abae738b45aa/html5/thumbnails/1.jpg)
Presented by:-Mr. Ajinkya NaharMr. Ajinkya NaharTwitter - @ajinkyanaharTwitter - @ajinkyanaharLinkedIn –LinkedIn –http://in.linkedin.com/pub/ajinkya-http://in.linkedin.com/pub/ajinkya-nahar/8/404/77bnahar/8/404/77b
![Page 2: PHP - PDO Objects](https://reader034.vdocuments.site/reader034/viewer/2022050805/559867e51a28abae738b45aa/html5/thumbnails/2.jpg)
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.
![Page 3: PHP - PDO Objects](https://reader034.vdocuments.site/reader034/viewer/2022050805/559867e51a28abae738b45aa/html5/thumbnails/3.jpg)
PDO Object and most useful statementThis 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 .
![Page 4: PHP - PDO Objects](https://reader034.vdocuments.site/reader034/viewer/2022050805/559867e51a28abae738b45aa/html5/thumbnails/4.jpg)
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.
![Page 5: PHP - PDO Objects](https://reader034.vdocuments.site/reader034/viewer/2022050805/559867e51a28abae738b45aa/html5/thumbnails/5.jpg)
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
![Page 6: PHP - PDO Objects](https://reader034.vdocuments.site/reader034/viewer/2022050805/559867e51a28abae738b45aa/html5/thumbnails/6.jpg)
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.
![Page 7: PHP - PDO Objects](https://reader034.vdocuments.site/reader034/viewer/2022050805/559867e51a28abae738b45aa/html5/thumbnails/7.jpg)
$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
![Page 8: PHP - PDO Objects](https://reader034.vdocuments.site/reader034/viewer/2022050805/559867e51a28abae738b45aa/html5/thumbnails/8.jpg)
Code ExamplesFor SELECT query -
$sql = 'SELECT * FROM users WHERE email = ? AND password = ?';
$pds = $pdo->prepare($sql);
$pds->execute(array(
$email,
$password
));
![Page 9: PHP - PDO Objects](https://reader034.vdocuments.site/reader034/viewer/2022050805/559867e51a28abae738b45aa/html5/thumbnails/9.jpg)
For INSERT query -
$sql = 'INSERT INTO users(email,password) VALUES(?,?)';
$pds = $pdo->prepare($sql);
$pds->execute(array($email,$password
));
![Page 10: PHP - PDO Objects](https://reader034.vdocuments.site/reader034/viewer/2022050805/559867e51a28abae738b45aa/html5/thumbnails/10.jpg)
For UPDATE query -
$sql = 'UPDATE users SET email=?, password=? WHERE id=?';
$pds = $pdo->prepare($sql);
$pds->execute(array($email,$password,$id
));
![Page 11: PHP - PDO Objects](https://reader034.vdocuments.site/reader034/viewer/2022050805/559867e51a28abae738b45aa/html5/thumbnails/11.jpg)
For DELETE query -
$sql = 'DELETE FROM users WHERE id = ?';
$pds = $pdo->prepare($sql);
$pds->execute(array($id
));
![Page 12: PHP - PDO Objects](https://reader034.vdocuments.site/reader034/viewer/2022050805/559867e51a28abae738b45aa/html5/thumbnails/12.jpg)
For like query -
$sql = 'SELECT * FROM users WHERE email like ?';
$pds = $pdo->prepare($sql);
$pds->execute(array(‘%$email%’
));
![Page 13: PHP - PDO Objects](https://reader034.vdocuments.site/reader034/viewer/2022050805/559867e51a28abae738b45aa/html5/thumbnails/13.jpg)