Thursday, 2 October 2014

How to use PHP data object?


PHP Data Object/PDO TOC Step By Step Tutorial PHP Data Object is a Database Connection Abstraction Library for PHP 5.

What is PDO?

  • a PHP5 extension written in a compiled language (C/C++)
  • a Lightweight DBMS connection abstract library (data access abstraction library)

Why PDO?

  • Support great number of database systems supported by PHP
  • You don't need rewriting of many line code for each database. Just write one and run anywhere
  • Speed. PDO written in compiled language, PHP libraries (ADOdb, PEAR DB) written in an interpreted language
  • Your software more easy to install. Do not need third party software

Whenever you need PDO?

  • You need portable application that support many database system
  • You need speed

Connect to MySQL

<?php
/*** mysql hostname ***/$hostname 'localhost';
/*** mysql username ***/$username 'username';
/*** mysql password ***/$password 'password';

try {
    
$dbh = new PDO("mysql:host=$hostname;dbname=mysql"$username$password);
    
/*** echo a message saying we have connected ***/
    
echo 'Connected to database';
    }
catch(
PDOException $e)
    {
    echo 
$e->getMessage();
    }
?>



Connect to Firebird

<?phptry {
    
$dbh = new PDO("firebird:dbname=localhost:C:\Programs\Firebird\DATABASE.FDB""SYSDBA""masterkey");
    }  
catch (
PDOException $e)
    {
    echo 
$e->getMessage();
    }
?>

Connect to Oracle

<?phptry {
    
$dbh = new PDO("OCI:""username""password")
    }
catch (
PDOException $e)
    {
    echo 
$e->getMessage();
    }
?>

Connect to SQLite

<?phptry {
    
/*** connect to SQLite database ***/
    
$dbh = new PDO("sqlite:/path/to/database.sdb");
    }
catch(
PDOException $e)
    {
    echo 
$e->getMessage();
    }
?>

No comments:

How calulation total value for HTML input text?

<script> $j(document).ready(function(){ $j(":text").keyup(function(){ if (isNaN($j(this).val())) { alert(...