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 no matter which database you use, you can use the same functions (methods) to query and retrieve data.
PDO was released with PHP 5.1 and is also available as a PECL extension for PHP 5.0. 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 shipped as shared extensions with PHP. To activate them, simply edit the `php.ini` file and add the following extension:
extension=php_pdo.dll
In addition, there are the following corresponding database-specific 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 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 to 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
";
/*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 you need a persistent database connection, add a final parameter: array(PDO::ATTR_PERSISTENT => true) to make it like this:
$db=new PDO($dsn,$user,$pass,array(PDO::ATTR_PERSISTENT=>true));
?>
Pretty simple, right? Next, let's take a detailed look at the specifics of PHP PDO:
* PDO Class:
β Initiates a transaction
β Commits a transaction
β Creates a PDO instance representing a database connection
β Gets the SQLSTATE associated with the last operation on the database handle
β Returns extended error information from the last operation on the database
β Executes an SQL statement and returns the number of affected rows
β Retrieves a database connection attribute
β Returns an array of available PDO drivers
β Checks if inside a transaction
β Returns the ID of the last inserted row, or the sequence value
β Prepares an SQL statement for execution and returns a PDOStatement object
β Executes an SQL statement, returning a PDOStatement object as a result set
β Quotes a string for use in a query.
β Rolls back a transaction
β Sets an attribute
* PDOStatement Class:
β Binds a column to a PHP variable
β Binds a parameter to the specified variable name
β Binds a value to a parameter
β Closes the cursor, enabling the statement to be executed again.
β Returns the number of columns in the result set
β Prints an SQL prepared command
β Gets the SQLSTATE associated with the last operation on the statement handle
β Gets extended error information associated with the last operation on the statement handle
β Executes a prepared statement
β Fetches the next row from a result set
β Returns an array containing all of the result set rows
β Returns a single column from the next row of a result set.
β Fetches the next row and returns it as an object.
β Retrieves a statement attribute
β Returns metadata for a column in a result set
β Advances to the next rowset in a multi-rowset statement handle
β Returns the number of rows affected by the last SQL statement
β Sets a statement attribute
β Sets the default fetch mode for a statement.
Php Pdo
π
2026-06-19 | π PHP
YouTip