PHP PDO介绍与应用

PDO扩展为PHP访问数据库定义了一个轻量级的、一致性的接口,它提供了一个数据访问抽象层,这样,无论使用什么数据库,都可以通过一致的函数执行查询和获取数据。PDO随PHP5.1发行,在PHP5.0的PECL扩展中也可以使用。

 

在windows环境下PHP5.1以上版本中:

  PDO和主要数据库的驱动同PHP一起作为扩展发布,要激活它们只需简单的编辑php.ini文件:   extension=php_pdo.dll   然后,选择针对特定数据库的DLL文件使用 dl() 在运行时加载,或者在php.ini文件中 php_pdo.dll 行后启用它们,如:   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   这些DLL文件应当存在于系统的 extension_dir 目录里。   注意 PDO_INFORMIX 只能作为一个PECL扩展使用。

 

类结构:

PDO {
__construct ( string $dsn [, string $username [, string $password [, array $driver_options ]]] )
bool beginTransaction ( void )
bool commit ( void )
mixed errorCode ( void )
array errorInfo ( void )
int exec ( string $statement )
mixed getAttribute ( int $attribute )
array getAvailableDrivers ( void )
array inTransaction ( void )
string lastInsertId ([ string $name = NULL ] )
PDOStatement prepare ( string $statement [, array $driver_options = array() ] )
PDOStatement query ( string $statement )
string quote ( string $string [, int $parameter_type = PDO::PARAM_STR ] )
bool rollBack ( void )
bool setAttribute ( int $attribute , mixed $value )
}

结构说明
  • PDO::beginTransaction — Initiates a transaction(初始事务)
  • PDO::commit — Commits a transaction(提交事务)
  • PDO::__construct — Creates a PDO instance representing a connection to a database(创建一个pdo与数据库的实例链接)
  • PDO::errorCode — Fetch the SQLSTATE associated with the last operation on the database handle
  • PDO::errorInfo — Fetch extended error information associated with the last operation on the database handle
  • PDO::exec — Execute an SQL statement and return the number of affected rows(执行sql并返回影响的sql条数)
  • PDO::getAttribute — Retrieve a database connection attribute
  • PDO::getAvailableDrivers — Return an array of available PDO drivers
  • PDO::inTransaction — Checks if inside a transaction
  • PDO::lastInsertId — Returns the ID of the last inserted row or sequence value
  • PDO::prepare — Prepares a statement for execution and returns a statement object
  • PDO::query — Executes an SQL statement, returning a result set as a PDOStatement object
  • PDO::quote — Quotes a string for use in a query.
  • PDO::rollBack — Rolls back a transaction(回滚)
  • PDO::setAttribute — Set an attribute
实例演示
Here is an singleton PDO example: ###### config.ini ###### db_driver=mysql db_user=root db_password=924892xp [dsn] host=localhost port=3306 dbname=localhost [db_options] PDO::MYSQL_ATTR_INIT_COMMAND=set names utf8 [db_attributes] ATTR_ERRMODE=ERRMODE_EXCEPTION ############ class Database { private static $link = null ; private static function getLink ( ) { if ( self :: $link ) { return self :: $link ; } $ini = _BASE_DIR . "config.ini" ; $parse = parse_ini_file ( $ini , true ) ; $driver = $parse [ "db_driver" ] ; $dsn = " ${ driver } :" ; $user = $parse [ "db_user" ] ; $password = $parse [ "db_password" ] ; $options = $parse [ "db_options" ] ; $attributes = $parse [ "db_attributes" ] ; foreach ( $parse [ "dsn" ] as $k => $v ) { $dsn .= " ${ k } = ${ v } ;" ; } self :: $link = new PDO ( $dsn , $user , $password , $options ) ; foreach ( $attributes as $k => $v ) { self :: $link -> setAttribute ( constant ( "PDO:: { $k } " ) , constant ( "PDO:: { $v } " ) ) ; } return self :: $link ; } public static function __callStatic ( $name , $args ) { $callback = array ( self :: getLink ( ), $name ) ; return call_user_func_array ( $callback , $args ) ; } } ?> // examples $stmt = Database :: prepare ( "SELECT 'something' ;" ) ; $stmt -> execute ( ) ; var_dump ( $stmt -> fetchAll ( ) ) ; $stmt -> closeCursor ( ) ; ?>

你可能感兴趣的:(PHP PDO介绍与应用)