What is PDO?
The PHP Data Objects (PDO) extension defines a lightweight, consistent interface for accessing databases in PHP. Each database driver that implements the PDO interface can expose database-specific features as regular extension functions. Note that you cannot perform any database functions using the PDO extension by itself; you must use a database-specific PDO driver to access a database server.
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. PDO does not provide a database abstraction; it doesn't rewrite SQL or emulate missing features. You should use a full-blown abstraction layer if you need that facility.
PDO ships with PHP 5.1, and is available as a PECL extension for PHP 5.0; PDO requires the new OO features in the core of PHP 5, and so will not run with earlier versions of PHP.
<? | |
//database connection $dbh = new PDO('mysql:host=localhost;dbname=clases', 'clase', 'clase', array(PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES \'UTF8\'')); | |
//parse statement $sth = $dbh->prepare("SELECT * FROM t"); | |
$sth->execute(); | |
/* Fetch all of the values of the first column */ | |
$result = $sth->fetchAll(PDO::FETCH_COLUMN, 0); | |
//var_dump($result); the code doesnt erase, it only will be comment. | |
echo json_encode($result); | |
$insert = $dbh->prepare("INSERT into t VALUES (?)"); | |
$insert->execute(array(1)); | |
$insert->execute(array(2)); | |
?> |
No comments:
Post a Comment