PHP PDO
PHP Data Objects (PDO) extension defines a lightweight, consistent interface PHP database access.
PDO provides a data access abstraction layer, which means that, no matter what kind of database, you can query and retrieve data using the same functions (methods).
PDO with PHP5.1 issued in PHP5.0 the PECL extension may also be used, you can not run on earlier versions of PHP.
PDO installation
You can PHP's phpinfo () function to see if the PDO extension is installed.
Installation on Unix systems PDO
In the Unix or Linux you need to add the following extensions:
extension=pdo.so
Windows users
PDO and all the major drivers as shared extensions released with PHP, to activate them simply edit your php.ini file and add the following extensions:
extension=php_pdo.dllIn addition there are the following extensions corresponding to various databases:
;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 set up these configurations, we need to restart PHP or Web server.
Next we look at their specific examples, the following examples use PDO to connect MySql database:
<?php $dbms='mysql'; //数据库类型 $host='localhost'; //数据库主机名 $dbName='test'; //使用的数据库 $user='root'; //数据库连接用户名 $pass=''; //对应的密码 $dsn="$dbms:host=$host;dbname=$dbName"; try { $dbh = new PDO($dsn, $user, $pass); //初始化一个PDO对象 echo "连接成功<br/>"; /*你还可以进行一次搜索操作 foreach ($dbh->query('SELECT * from FOO') as $row) { print_r($row); //你可以用 echo($GLOBAL); 来看到这些值 } */ $dbh = null; } catch (PDOException $e) { die ("Error!: " . $e->getMessage() . "<br/>"); } //默认这个不是长连接,如果需要数据库长连接,需要最后加一个参数:array(PDO::ATTR_PERSISTENT => true) 变成这样: $db = new PDO($dsn, $user, $pass, array(PDO::ATTR_PERSISTENT => true)); ?>
Very simple, then let us look at the specific PHP PDO specify:
- Predefined Constants
- PHP PDO connection manager
- PHP PDO Affairs and auto-commit
- PHP PDO prepared statements and stored procedures
- PHP PDO error with error handling
- PHP PDO large objects (LOBs)
- PDO categories:
- :: BeginTransaction the PDO - start a transaction
- :: The commit the PDO - commit a transaction
- :: __ Construct PDO - Creates a PDO instance database connection
- :: ErrorCode the PDO - Get with the last operation on the database handle associated SQLSTATE
- :: ErrorInfo the PDO - returns an error message database last action
- :: Exec the PDO - execute a SQL statement and returns the number of affected rows
- :: The getAttribute the PDO - Retrieve a database connection attribute
- :: GetAvailableDrivers the PDO - returns an array of available drivers
- :: The inTransaction the PDO - checks whether a transaction within
- :: LastInsertId the PDO - ID or sequence value returns the last inserted row
- :: PREPARE the PDO - prepare the SQL statement to be executed and returned a PDOStatement objects
- :: Query the PDO - SQL statement is executed, returns PDOStatement object that can be understood as the result set
- :: Quote the PDO - add quotes in the SQL statement string.
- :: RollBack the PDO - to roll back a transaction
- :: The setAttribute the PDO - Setting properties
- PDOStatement categories:
- :: BindColumn PDOStatement - bound one to one PHP variable
- :: To bindParam PDOStatement - bind a parameter to the specified variable name
- :: BindValue PDOStatement - bind a value to a parameter
- :: CloseCursor PDOStatement - close the cursor, so that the statement can be executed again.
- :: ColumnCount PDOStatement - Returns the number of columns in the result set
- :: DebugDumpParams PDOStatement - print a SQL command preprocessing
- :: ErrorCode PDOStatement - Get a handle to keep up with a statement related to the operation of the SQLSTATE
- :: ErrorInfo PDOStatement - Get a handle to keep up with a statement related to the operation of extended error information
- :: The Execute PDOStatement - execute a prepared statement
- :: FETCH PDOStatement - focus gets the next row from the result
- :: FetchAll PDOStatement - returns an array of all the rows in the result set
- :: FetchColumn PDOStatement - from the next row in the result set to return a single one.
- :: FetchObject PDOStatement - Get the next row and returns as an object.
- :: The getAttribute PDOStatement - retrieve a statement attribute
- :: GetColumnMeta PDOStatement - Returns result set metadata in a
- :: NextRowset PDOStatement - advance to the next set of rows in a multi-line set in the statement handle
- :: The rowCount PDOStatement - a return by the SQL statement impact of the number of rows
- :: The setAttribute PDOStatement - Set a statement attribute
- :: SetFetchMode PDOStatement - set the default acquisition mode for the statement.