11.1 PHP - Data - Object - PDO
11.1 PHP - Data - Object - PDO
What is PDO
a PHP5 extension written in a compiled language (C/C++)
a Lightweight DBMS connection abstract library (data access
abstraction library)
while($row = $result->fetch()) {
echo $row['Module_Code'] ." ";
echo $row['Module_Name'] ."\n";
}
FETCH OBJ example
This fetch type creates an object of std class for each row of
fetched data.
$result = $connection->query('SELECT
Module_Code,Module_Name from modules');
# setting the fetch mode
$result->setFetchMode(PDO::FETCH_OBJ);
while($row = $result->fetch()) {
echo $row->Module_Code;
echo $row->Module_Name;
}
Insert and Update
Inserting new data, or updating existing data is one of the
more common database operations.
Using PDO, this is normally a two-step process. Everything
covered in this section applies equally to both UPDATE and
INSERT operations.
EG: Basic Insert operation
# STH means "Statement Handle"
$STH = $DBH-
>prepare("INSERT INTO folks ( first_name ) values ( 'Cathy'
)");
$STH->execute();
# the shortcut!
$STH = $DBH->("INSERT INTO folks (name, addr, city)
value (:name, :addr, :city)");
$STH->execute($data);
References
http://net.tutsplus.com/tutorials/php/why-you-should-be-
using-phps-pdo-for-database-access/
http://www.phpeveryday.com/articles/PDO-Positional-
and-Named-Placeholders-P551.html