Nov 30 2013
PHP MySQL PDO Example
PHP MySQL PDO Database Example
The PDO
The PHP Data Object(PDO) provides speedy and lightweight interface for connecting PHP to MySQL databases. We can access any database server using PDO extension but we must use a DBMS specific PDO driver.
PDO provides, database abstraction layer means we can use any database server with the same set of functions. PDO requires the new OO features in the core of PHP 5, and so will not run with earlier versions of PHP.
PDO Activation
Step – 1
PDO and all the major drivers ship with PHP as shared extensions, and simply need to be activated by editing the php.ini file:
extension=php_pdo.dll
Step – 2
Next, choose the other database-specific DLL files and either use dl() to load them at runtime, or enable them in php.ini below php_pdo.dll.
PHP MySQL PDO Example
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | <?php try { $dbh = new PDO('mysql:host=localhost;dbname=test', $user, $pass); //Connecting MySQL foreach($dbh->query('SELECT * from FOO') as $row) { print_r($row); } $dbh = null; //Closing MySQL } catch (PDOException $e) { print "Error!: " . $e->getMessage() . "<br/>"; die(); } ?> |
$dsn
[, string $username
[, string $password
[, array $driver_options
]]] )$statement
)$attribute
)$statement
)}