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:
-PHP PDO Connection Management
-PHP PDO Transactions and Auto-commit
-PHP PDO Prepared Statements and Stored Procedures
-PHP PDO Error and Error Handling
- PDO Class:
-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::getAttribute — Retrieves a database connection attribute
PDO::getAvailableDrivers — Returns an array of available drivers
PDO::inTransaction — Checks if inside a transaction
PDO::lastInsertId — Returns the ID of the last inserted row or sequence value
PDO::prepare — Prepares an SQL statement and returns a PDOStatement object
PDO::query — Executes an SQL statement, returning a PDOStatement object as a result set
PDO::quote — Quotes a string for use in a query
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 the specified variable name
PDOStatement::bindValue — Binds a value to a parameter
PDOStatement::closeCursor — Closes the cursor, enabling the statement to be executed again
PDOStatement::columnCount — Returns the number of columns in the result set
PDOStatement::debugDumpParams — Dumps the SQL prepared command
PDOStatement::errorCode — Retrieves the SQLSTATE associated with the last operation on the statement handle
PDOStatement::errorInfo — Retrieves 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::fetchColumn — Returns a single column from the next row of a result set
PDOStatement::fetchObject — Fetches the next row and returns it as an object
PDOStatement::getAttribute — Retrieves a statement attribute
PDOStatement::getColumnMeta — Returns metadata for a column in a 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