XCVIII. PDO Functions

Introduction

Warning

This extension is EXPERIMENTAL. The behaviour of this extension -- including the names of its functions and anything else documented about this extension -- may change without notice in a future release of PHP. Use this extension at your own risk.

The PHP Data Objects (PDO) extension defines a lightweight, consistent interface for accessing databases in PHP. Each database driver that implements the PDO interface can expose database-specific features as regular extension functions. Note that you cannot perform any database functions using the PDO extension by itself; you must use a database-specific PDO driver to access a database server.

Installation

Windows

Follow the same steps to install and enable the PDO drivers of your choice.

  1. Windows users can download the extension DLL php_pdo.dll as part of the PECL collection binaries from http://www.php.net/downloads.php or a more recent version from a PHP 5 PECL Snapshot.

  2. To enable the PDO extension on Windows operating systems, you must add the following line to php.ini:
    extension=php_pdo.dll

  3. Next, choose the other DB specific DLL files and either use dl() to load them at runtime, or enable them in php.ini below pdo_pdo.dll. For example:
    extension=php_pdo.dll
    extension=php_pdo_firebird.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

    These DLL's should exist in the systems extension_dir.

Linux and UNIX

Due to a bug in the pear installer you should install the PDO package manually using the following steps:

Follow the same steps to install and enable the PDO drivers of your choice.

  1. Download the PDO package to your local machine:
    bash$  wget http://pecl.php.net/get/PDO

  2. Determine your PHP bin directory. If your PHP 5 CLI binary lives at /usr/local/php5/bin/php then the bin dir is /usr/local/php5/bin.

  3. Set your path so that your PHP bin directory is at the front:
    export PATH="/usr/local/php5/bin:$PATH"

  4. Manually build and install the PDO extension:
    bash$ tar xzf PDO-0.2.tgz
    bash$ cd PDO-0.2
    bash$ phpize
    bash$ ./configure
    bash$ make
    bash$ sudo -s
    bash# make install
    bash# echo extension=pdo.so >> /usr/local/php5/lib/php.ini

PDO Drivers

The following drivers currently implement the PDO interface:

Driver nameSupported databases
PDO_DBLIBFreeTDS / Microsoft SQL Server / Sybase
PDO_FIREBIRDFirebird/Interbase 6
PDO_MYSQLMySQL 3.x/4.0
PDO_OCIOracle Call Interface
PDO_ODBCODBC v3 (IBM DB2 and unixODBC)
PDO_PGSQLPostgreSQL
PDO_SQLITESQLite 3.x

Predefined Classes

PDO

Represents a connection between PHP and a database server.

Constructor

  • PDO - constructs a new PDO object

Methods

  • beginTransaction - begins a transaction

  • commit - commits a transaction

  • exec - issues an SQL statement and returns the number of affected rows

  • errorCode - retrieves an error code, if any, from the database

  • errorInfo - retrieves an array of error information, if any, from the database

  • getAttribute - retrieves a database connection attribute

  • lastInsertId - retrieves the value of the last row that was inserted into a table

  • prepare - prepares an SQL statement for execution

  • query - issues an SQL statement and returns a result set

  • quote - returns a quoted version of a string for use in SQL statements

  • rollBack - roll back a transaction

  • setAttribute - sets a database connection attribute

PDOStatement

Represents a prepared statement and, after the statement is executed, an associated result set.

Methods

  • bindColumn - binds a PHP variable to an output column in a result set

  • bindParam - binds a PHP variable to a parameter in the prepared statement

  • columnCount - returns the number of columns in the result set

  • errorCode - retrieves an error code, if any, from the statement

  • errorInfo - retrieves an array of error information, if any, from the statement

  • execute - executes a prepared statement

  • fetch - fetches a row from a result set

  • fetchAll - fetches an array containing all of the rows from a result set

  • fetchSingle - returns the data from the first column in a result set

  • getAttribute - retrieves a PDOStatement attribute

  • getColumnMeta - retrieves metadata for a column in the result set

  • rowCount - returns the number of rows that were affected by the execution of an SQL statement

  • setAttribute - sets a PDOStatement attribute

  • setFetchMode - sets the fetch mode for a PDOStatement

Predefined Constants

The constants below are defined by this extension, and will only be available when the extension has either been compiled into PHP or dynamically loaded at runtime.

PDO_PARAM_NULL (integer)

Represents the SQL NULL data type.

PDO_PARAM_INT (integer)

Represents the SQL INTEGER data type.

PDO_PARAM_STR (integer)

Represents the SQL CHAR, VARCHAR, or other string data type.

PDO_PARAM_LOB (integer)

Represents the SQL large object data type.

PDO_PARAM_STMT (integer)

PDO_PARAM_INPUT_OUTPUT (integer)

Specifies that the parameter is an INOUT parameter for a stored procedure. You must bitwise-OR this value with an explicit PDO_PARAM_* data type.

PDO_FETCH_LAZY (integer)

Specifies that the fetch method shall return each row as an object with variable names that correspond to the column names returned in the result set. PDO_FETCH_LAZY creates the object variable names as they are accessed.

PDO_FETCH_ASSOC (integer)

Specifies that the fetch method shall return each row as an array indexed by column name as returned in the corresponding result set.

PDO_FETCH_NUM (integer)

Specifies that the fetch method shall return each row as an array indexed by column number as returned in the corresponding result set, starting at column 0.

PDO_FETCH_BOTH (integer)

Specifies that the fetch method shall return each row as an array indexed by both column name and number as returned in the corresponding result set, starting at column 0.

PDO_FETCH_OBJ (integer)

Specifies that the fetch method shall return each row as an object with property names that correspond to the column names returned in the result set.

PDO_FETCH_BOUND (integer)

Specifies that the fetch method shall return TRUE and assign the values of the columns in the result set to the PHP variables to which they were bound with the PDOStatement::bindParam() or PDOStatement::bindColumn() methods.

PDO_FETCH_COLUMN (integer)

Specifies that the fetch method shall return only a single requested column from the next row in the result set.

PDO_FETCH_CLASS (integer)

Specifies that the fetch method shall return a new instance of the requested class, mapping the columns to named properties in the class.

PDO_FETCH_INTO (integer)

Specifies that the fetch method shall update an existing instance of the requested class, mapping the columns to named properties in the class.

PDO_FETCH_FUNC (integer)

PDO_FETCH_GROUP (integer)

PDO_FETCH_UNIQUE (integer)

PDO_FETCH_CLASSTYPE (integer)

PDO_ATTR_AUTOCOMMIT (integer)

PDO_ATTR_PREFETCH (integer)

PDO_ATTR_TIMEOUT (integer)

PDO_ATTR_ERRMODE (integer)

PDO_ATTR_SERVER_VERSION (integer)

PDO_ATTR_CLIENT_VERSION (integer)

PDO_ATTR_SERVER_INFO (integer)

PDO_ATTR_CONNECTION_STATUS (integer)

PDO_ATTR_CASE (integer)

Force column names to a specific case specified by the PDO_CASE_* constants.

PDO_ATTR_CURSOR_NAME (integer)

PDO_ATTR_CURSOR (integer)

PDO_ATTR_ORACLE_NULLS (integer)

PDO_ATTR_PERSISTENT (integer)

PDO_ERRMODE_SILENT (integer)

PDO_ERRMODE_WARNING (integer)

PDO_ERRMODE_EXCEPTION (integer)

PDO_CASE_NATURAL (integer)

Leave column names as returned by the database driver.

PDO_CASE_LOWER (integer)

Force column names to lower case.

PDO_CASE_UPPER (integer)

Force column names to upper case.

PDO_FETCH_ORI_NEXT (integer)

Fetch the next row in the result set. Valid only for scrollable cursors.

PDO_FETCH_ORI_PRIOR (integer)

Fetch the previous row in the result set. Valid only for scrollable cursors.

PDO_FETCH_ORI_FIRST (integer)

Fetch the first row in the result set. Valid only for scrollable cursors.

PDO_FETCH_ORI_LAST (integer)

Fetch the last row in the result set. Valid only for scrollable cursors.

PDO_FETCH_ORI_ABS (integer)

Fetch the requested row by row number from the result set. Valid only for scrollable cursors.

PDO_FETCH_ORI_REL (integer)

Fetch the requested row by relative position from the current position of the cursor in the result set. Valid only for scrollable cursors.

PDO_CURSOR_FWDONLY (integer)

Create a PDOStatement object with a forward-only cursor. This may improve the performance of your application but restricts your PDOStatement object to fetching one row at a time from the result set in a forward direction.

PDO_CURSOR_SCROLL (integer)

Create a PDOStatement object with a scrollable cursor. Pass the PDO_FETCH_ORI_* constants to control the rows fetched from the result set.

PDO_ERR_CANT_MAP (integer)

PDO_ERR_SYNTAX (integer)

PDO_ERR_CONSTRAINT (integer)

PDO_ERR_NOT_FOUND (integer)

PDO_ERR_ALREADY_EXISTS (integer)

PDO_ERR_NOT_IMPLEMENTED (integer)

PDO_ERR_MISMATCH (integer)

PDO_ERR_TRUNCATED (integer)

PDO_ERR_DISCONNECTED (integer)

PDO_ERR_NO_PERM (integer)

PDO_ERR_NONE (string)

Corresponds to SQLSTATE '00000', meaning that the SQL statement was successfully issued with no errors or warnings.

Table of Contents
PDO::beginTransaction --  Initiates a transaction
PDO::commit --  Commits a transaction
PDO::__construct --  Creates a PDO instance representing a connection to a database
PDO::errorCode --  Fetch the SQLSTATE associated with the last operation on the database handle
PDO::errorInfo --  Fetch extended error information associated with the last operation on the database handle
PDO::exec --  Execute an SQL statement and return the number of affected rows
PDO::getAttribute --  Retrieve a database connection attribute
PDO::lastInsertId --  Returns the ID of the last inserted row
PDO::prepare --  Prepares a statement for execution and returns a statement object
PDO::query --  Executes an SQL statement, returning a result set as a PDOStatement object
PDO::quote --  Quotes a string for use in a query.
PDO::rollBack --  Rolls back a transaction
PDO::setAttribute --  Set an attribute
PDOStatement::bindColumn --  Bind a column to a PHP variable
PDOStatement::bindParam --  Binds a parameter to a the specified variable name
PDOStatement::columnCount --  Returns the number of columns in the result set
PDOStatement::errorCode --  Fetch the SQLSTATE associated with the last operation on the statement handle
PDOStatement::errorInfo --  Fetch extended error information associated with the last operation on the statement handle
PDOStatement::execute --  Executes a prepared statement
PDOStatement::fetch --  Fetches the next row from a result set
PDOStatement::fetchAll --  Returns an array containing all of the result set rows
PDOStatement::fetchSingle --  Returns the first column in the next row of a result set
PDOStatement::getAttribute --  Retrieve a statement attribute
PDOStatement::getColumnMeta --  Returns metadata for a column in a result set
PDOStatement::rowCount --  Returns the number of rows affected by the last SQL statement
PDOStatement::setAttribute --  Set a statement attribute
PDOStatement::setFetchMode --  Set the default fetch mode for this statement