This document summarizes the history of PHP persistence from 1995 to present day. It begins with early file handling in PHP/FI in 1995 and the introduction of database support. It then discusses the evolution of code reusability through functions and classes. Professional abstraction layers like PEAR and later ORM frameworks provided more robust and standardized APIs. NoSQL databases and drivers were later incorporated, moving beyond relational databases. Current frameworks provide object document mapping for non-SQL databases like MongoDB.
12. function db_connect($database, $host, $user, $pwd);
function db_fetch_single($dbh, $query);
function db_fetch_all($dbh, $query);
function db_insert($dbh, $table, $values);
function db_update($dbh, $table, $values, $pk);
function db_delete($dbh, $table, $pk);
function db_list_fields($dbh, $table);
function db_drop_table($dbh, $table);
14. /**
* Fetches a single record from a database
*
* @param resource $dbh The database handler
* @param string $query A SQL query
* @return array A single record as an array
*/
function db_fetch_single($dbh, $query)
{
return current(db_fetch_all($dbh, $query));
}
19. class Database
{
function Database($database, host, $user, $pwd);
function disconnect();
function connect();
function free();
function getErrorMessage();
function getErrorCode();
function getLastInsertId();
function insert($table, array $values);
function fetchAll($query);
function fetchSingle($query);
function query($query);
function quote($string);
function hasError();
function _collectError();
function _init();
}
20. require 'lib/Database.php';
$db= &new Database('demo', 'localhost', 'root');
$q = 'SELECT id, name FROM author';
foreach ($db->fetchAll($q) as $author) {
// ...
}
$db->free();
$db->disconnect();
21. require 'lib/Database.php';
$db= &new Database('demo', 'localhost', 'root');
$q = 'SELECT id, name FROM author WHERE id = 1';
$author = $db->fetchSingle($q);
$db->free();
$db->disconnect();
33. • PECL extension as of PHP 5.0.0
• Core extension since PHP 5.1.0 (11/2005)
• 12 official drivers as of today
• Extensible Object Oriented API
• Prepared statements & transaction support
• Stored procedures support
34.
35. « PDO provides a data-access abstraction
layer, which means that, regardless of
which database you're using, you use the
same functions to issue queries and fetch
data. »
36. « PDO does not provide a
database abstraction as it
doesn't rewrite SQL or emulate
missing features »
50. • Use objects instead of raw SQL queries
• Database abstraction layer (not always)
• Relationships support
• Behaviors support (i18n, timestampable…)
• Querying API
• Error logging
68. • Cross-platform support
• Schemaless language
• BSON type support
• Binary le storage support (GridFS)
• Master – slave replication support
• Sharding (horizontal scaling)
69. $mongo = new Mongo();
$col = $mongo->selectDb('osidays')->books;
$books = array(
array('title' => 'Da Vinci Code'),
array('title' => 'The Lost Symbol'),
array('title' => 'Digital Fortress'),
);
foreach ($books as $book) {
$collection->insert($book);
}