PDO Basics

Leave a Comment
PDO stands for PHP Data Objects and it is used for connecting your php program to any  database you want. 
Although it is enabled by default from version PHP 5.1.0, i would like to tell how to enable PDO if  in case PDO is not enabled by default.

 You  just need to remove comment from php.ini

;extension=php_pdo.dll

 Now you can choose any database specific DLL files or you can also load them at runtime.

extension=php_pdo.dll
extension=php_pdo_firebird.dll
extension=php_pdo_informix.dll
extension=php_pdo_mssql.dll
extension=php_pdo_mysql.dll
extension=php_pdo_oci.dll
extension=php_pdo_oci8.dll
extension=php_pdo_odbc.dll
extension=php_pdo_pgsql.dll
extension=php_pdo_sqlite.dll 


NOTE : all these DLLs are available in extension_dir


Load PDO Database specific DLL  at runtime


<?php
if (!extension_loaded('sqlite')) {
    if (strtoupper(substr(PHP_OS, 0, 3)) === 'WIN') {
        dl('php_sqlite.dll');
    } else {
        dl('sqlite.so');
    }
}


?>
Now you may have enabled PDO on your machine.
You can connect to database using following code.


<?php

$dbh = new PDO('mysql:host=localhost;dbname=test', $user, $pass);

?>

To close database connection just do this


<?php

$dbh = null;

?>

 



0 comments:

Post a Comment