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_Main

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

PDO Class

PDO {
public __construct ( string $dsn [, string $username [, string $password [, array $driver_options ]]] )
public bool beginTransaction ( void )
public bool commit ( void )
public mixed errorCode ( void )
public array errorInfo ( void )
public int exec ( string $statement )
public mixed getAttribute ( int $attribute )
public static array getAvailableDrivers ( void )
public bool inTransaction ( void )
public string lastInsertId ([ string $name = NULL ] )
public PDOStatement prepare ( string $statement [, array $driver_options = array() ] )
public PDOStatement query ( string $statement )
public string quote ( string $string [, int $parameter_type = PDO::PARAM_STR ] )
public bool rollBack ( void )
public bool setAttribute ( int $attribute , mixed $value )

}