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:
- Predefined Constants
- PHP PDO Connection Management
- PHP PDO Transactions and Auto-Commit
- PHP PDO Prepared Statements and Stored Procedures
- PHP PDO Errors and Error Handling
- PHP PDO Large Objects (LOBs)
- PDO class:
- PDO::beginTransaction — starts 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 information about the last database operation error
- PDO::exec — executes an SQL statement and returns the number of affected rows
- PDO::getAttribute — retrieves an attribute of a database connection
- PDO::getAvailableDrivers — returns an array of available drivers
- PDO::inTransaction — checks whether inside a transaction
- PDO::lastInsertId — returns the ID or sequence value of the last inserted row
- PDO::prepare — prepares an SQL statement to be executed and returns a PDOStatement object
- PDO::query — executes an SQL statement, returning a PDOStatement object (i.e. a result set)
- PDO::quote — quotes a string for use in an SQL statement.
- PDO::rollBack — rolls back a transaction
- PDO::setAttribute — sets an attribute
- PDOStatement class:
- PDOStatement::bindColumn — binds a column to a PHP variable
- PDOStatement::bindParam — binds a parameter to a specified variable name
- PDOStatement::bindValue — binds a value to a parameter
- PDOStatement::closeCursor — closes the cursor, allowing the statement to be executed again.
- PDOStatement::columnCount — returns the number of columns in the result set
- PDOStatement::debugDumpParams — dumps the information contained in a prepared command
- PDOStatement::errorCode — retrieves the SQLSTATE associated with the last statement handle operation
- PDOStatement::errorInfo — retrieves the extended error information associated with the last statement handle operation
- PDOStatement::execute — executes a prepared statement
- PDOStatement::fetch — fetches the next row from the result set
- PDOStatement::fetchAll — returns an array containing all rows in the result set
- PDOStatement::fetchColumn — returns a single column from the next row of the result set.
- PDOStatement::fetchObject — fetches the next row and returns it as an object.
- PDOStatement::getAttribute — retrieves a statement attribute
- PDOStatement::getColumnMeta — returns the metadata for a column in the result set
- PDOStatement::nextRowset — advances to the next rowset in a multi-rowset statement handle
- PDOStatement::rowCount — returns the number of rows affected by the last SQL statement
- PDOStatement::setAttribute — sets a statement attribute
- PDOStatement::setFetchMode — sets the default fetch mode for the statement.