Easy Tutorial
❮ Pdo Construct Func Curl_Multi_Setopt ❯

PHP PDO

The PHP Data Objects (PDO) extension defines a lightweight, consistent interface for accessing databases in PHP.

PDO provides a data access abstraction layer, which means that, regardless of which database you are using, you can use the same functions (methods) to query and fetch data.

PDO was released with PHP 5.1 and is also available in the PECL extension for PHP 5.0, but it cannot run on earlier versions of PHP.


PDO Installation

You can check if the PDO extension is installed by using PHP's phpinfo() function.

Installing PDO on Unix Systems

On Unix or Linux, you need to add the following extension:

extension=pdo.so

Windows Users

PDO and all major drivers are released as shared extensions with PHP. To activate them, simply edit the php.ini file and add the following extensions:

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

After setting these configurations, you need to restart PHP or the web server.

Next, let's look at a specific example, here is an example of using PDO to connect to a MySQL database:

Example

<?php
$dbms='mysql';     // Database type
$host='localhost'; // Database host name
$dbName='test';    // Database in use
$user='root';      // Database connection username
$pass='';          // Corresponding password
$dsn="$dbms:host=$host;dbname=$dbName";

try {
    $dbh = new PDO($dsn, $user, $pass); // Initialize a PDO object
    echo "Connection successful<br/>";
    /* You can also perform a search operation
    foreach ($dbh->query('SELECT * from FOO') as $row) {
        print_r($row); // You can use echo($GLOBAL); to see these values
    }
    */
    $dbh = null;
} catch (PDOException $e) {
    die ("Error!: " . $e->getMessage() . "<br/>");
}
// By default, this is not a long connection. If you need a long database connection, you need to add a parameter: array(PDO::ATTR_PERSISTENT => true) like this:
$db = new PDO($dsn, $user, $pass, array(PDO::ATTR_PERSISTENT => true));

?>

That's simple, isn't it? Now, let's take a detailed look at PHP PDO:

-Predefined Constants

-PHP PDO Connection Management

-PHP PDO Transactions and Auto-commit

-PHP PDO Prepared Statements and Stored Procedures

-PHP PDO Error and Error Handling

-PHP PDO Large Objects (LOBs)

-PDO::beginTransaction — Initiates a transaction

-PDO::commit — Commits a transaction

-PDO::__construct — Creates a PDO instance representing a database connection

-PDO::errorCode — Retrieves the SQLSTATE associated with the last operation on the database handle

-PDO::errorInfo — Returns the error information for the last operation on the database

-PDO::exec — Executes an SQL statement and returns the number of affected rows

❮ Pdo Construct Func Curl_Multi_Setopt ❯