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 no matter which database is used, the same functions (methods) can be used to query and obtain data.
PDO is released with PHP5.1 and can also be used in the PECL extension of PHP5.0, but cannot run on previous PHP versions.
You can check whether the PDO extension is installed through PHP's phpinfo() function.
On Unix or Linux you need to add the following extension:
extension=pdo.so
PDO and all major drivers are shipped with PHP as shared extensions. To activate them simply edit the php.ini file and add the following extension:
extension=php_pdo.dllIn addition, there are various database extensions corresponding to the following:
;extension=php_pdo_firebird.dll;extension=php_pdo_informix.dll;extension=php_pdo_mssql.dll;extension=php_pdo_mysql.dll;extensio n=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 take a look at specific examples. The following is an example of using PDO to connect to a MySql database:
<?php$dbms='mysql'; //Database type $host='localhost'; //Database host name $dbName='test'; //Database used $user='root'; //Database connection user Name $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 connection to the database, you need to add a parameter at the end: array(PDO::ATTR_PERSISTENT => true) It becomes like this: $db = new PDO( $dsn, $user, $pass, array(PDO::ATTR_PERSISTENT => true));?>
It's very simple. Next, let us take a closer look at the specific instructions of PHP PDO:
Predefined constants
PHP PDO connection connection management
PHP PDO transactions and automatic commit
PHP PDO prepared statements and stored procedures
PHP PDO errors and error handling
PHP PDO Large Objects (LOBs)
PDO class:
PDO::beginTransaction — start a transaction
PDO::commit — Commit a transaction
PDO::__construct — Create a PDO instance that represents a database connection
PDO::errorCode — Get the SQLSTATE associated with the last operation of the database handle
PDO::errorInfo — Returns the error information of the last database operation
PDO::exec — Execute a SQL statement and return the number of affected rows
PDO::getAttribute — Retrieve attributes of a database connection
PDO::getAvailableDrivers — Returns an array of available drivers
PDO::inTransaction — Check if within a transaction
PDO::lastInsertId — Returns the ID or sequence value of the last inserted row
PDO::prepare — Prepares a SQL statement to be executed and returns a PDOStatement object
PDO::query — Execute SQL statements and return PDOStatement objects, which can be understood as result sets
PDO::quote — Add quotes to strings in SQL statements.
PDO::rollBack — roll back a transaction
PDO::setAttribute — set an attribute
PDOStatement class:
PDOStatement::bindColumn — Bind a column to a PHP variable
PDOStatement::bindParam — Bind a parameter to the specified variable name
PDOStatement::bindValue — Bind a value to a parameter
PDOStatement::closeCursor — Closes the cursor so that the statement can be executed again.
PDOStatement::columnCount — Returns the number of columns in the result set
PDOStatement::debugDumpParams — Print a SQL preprocessing command
PDOStatement::errorCode — Get the SQLSTATE associated with the last statement handle operation
PDOStatement::errorInfo — Get extended error information related to the last statement handle operation
PDOStatement::execute — execute a prepared statement
PDOStatement::fetch — Get 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 in the result set.
PDOStatement::fetchObject — Gets the next row and returns it as an object.
PDOStatement::getAttribute — Retrieve a statement attribute
PDOStatement::getColumnMeta — Returns metadata for a column in a result set
PDOStatement::nextRowset — Advance to the next rowset in a multi-rowset statement handle
PDOStatement::rowCount — Returns the number of rows affected by the previous SQL statement
PDOStatement::setAttribute — Set a statement attribute
PDOStatement::setFetchMode — Set the default fetch mode for statements.