Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

PHP PDO

PHP PDO

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

PDO provides a data access abstraction layer, which means that regardless of which database is used, you can query and retrieve data using the same functions (methods).

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


PDO Installation

You can use PHP's phpinfo() function to check whether the PDO extension is installed.

Installing PDO on Unix systems

On Unix or Linux you need to add the following extensions:



extension=pdo.so

Windows users

PDO and all major drivers are released with PHP as shared extensions; to enable them simply edit the php.ini file and add the following extensions:



extension=php_pdo.dll

In addition, there are the following corresponding database extensions:





;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, we need to restart PHP or the web server.

Next, let's look at a concrete example; the following is an example of connecting to a MySQL database using PDO:

Example

" ; /*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 ( ) . "
" ) ; } //By default this is not a persistent connection; if a persistent database connection is needed, add the following parameter at the end: array(PDO::ATTR_PERSISTENT => true) becomes: $db = new PDO ( $dsn , $user , $pass , array ( PDO :: ATTR_PERSISTENT => true ) ) ; ?>

Simple, right? Next, let's take a detailed look at the PHP PDO reference:

- [PDO::beginTransaction](pdo-begintransaction.html) — starts a transaction - [PDO::commit](pdo-commit.html) — commits a transaction - [PDO::__construct](pdo-construct.html) — creates a PDO instance representing a database connection - [PDO::errorCode](pdo-errorcode.html) — retrieves the SQLSTATE associated with the last operation on the database handle - [PDO::errorInfo](pdo-errorinfo.html) — returns information about the last database operation error - [PDO::exec](pdo-exec.html) — executes an SQL statement and returns the number of affected rows - [PDO::getAttribute](pdo-getattribute.html) — retrieves an attribute of a database connection - [PDO::getAvailableDrivers](pdo-getavailabledrivers.html) — returns an array of available drivers - [PDO::inTransaction](pdo-intransaction.html) — checks whether inside a transaction - [PDO::lastInsertId](pdo-lastinsertid.html) — returns the ID or sequence value of the last inserted row - [PDO::prepare](pdo-prepare.html) — prepares an SQL statement to be executed and returns a PDOStatement object - [PDO::query](pdo-query.html) — executes an SQL statement, returning a PDOStatement object (i.e. a result set) - [PDO::quote](pdo-quote.html) — quotes a string for use in an SQL statement. - [PDO::rollBack](pdo-rollback.html) — rolls back a transaction - [PDO::setAttribute](pdo-setattribute.html) — sets an attribute - [PDOStatement::bindColumn](pdostatement-bindcolumn.html) — binds a column to a PHP variable - [PDOStatement::bindParam](pdostatement-bindparam.html) — binds a parameter to a specified variable name - [PDOStatement::bindValue](pdostatement-bindvalue.html) — binds a value to a parameter - [PDOStatement::closeCursor](pdostatement-closecursor.html) — closes the cursor, allowing the statement to be executed again. - [PDOStatement::columnCount](pdostatement-columncount.html) — returns the number of columns in the result set - [PDOStatement::debugDumpParams](pdostatement-debugdumpparams.html) — dumps the information contained in a prepared command - [PDOStatement::errorCode](pdostatement-errorcode.html) — retrieves the SQLSTATE associated with the last statement handle operation - [PDOStatement::errorInfo](pdostatement-errorinfo.html) — retrieves the extended error information associated with the last statement handle operation - [PDOStatement::execute](pdostatement-execute.html) — executes a prepared statement - [PDOStatement::fetch](pdostatement-fetch.html) — fetches the next row from the result set - [PDOStatement::fetchAll](pdostatement-fetchall.html) — returns an array containing all rows in the result set - [PDOStatement::fetchColumn](pdostatement-fetchcolumn.html) — returns a single column from the next row of the result set. - [PDOStatement::fetchObject](pdostatement-fetchobject.html) — fetches the next row and returns it as an object. - [PDOStatement::getAttribute](pdostatement-getattribute.html) — retrieves a statement attribute - [PDOStatement::getColumnMeta](pdostatement-getcolumnmeta.html) — returns the metadata for a column in the result set - [PDOStatement::nextRowset](pdostatement-nextrowset.html) — advances to the next rowset in a multi-rowset statement handle - [PDOStatement::rowCount](pdostatement-rowcount.html) — returns the number of rows affected by the last SQL statement - [PDOStatement::setAttribute](pdostatement-setattribute.html) — sets a statement attribute - [PDOStatement::setFetchMode](pdostatement-setfetchmode.html) — sets the default fetch mode for the statement.