PHP中的PDO详解

WBOY
Release: 2016-06-20 12:47:04
Original
991 people have browsed it

PDO 是一个“数据库访问抽象层”,作用是统一各种数据库(MySQL、MSSQL、Oracle、DB2、PostgreSQL……)的访问接口,能轻松的在不同的数据库之间完成切换,使得数据库间的移植容易实现。

PDO常用方法:
PDO::query()主要用于有记录结果返回的操作(PDOStatement),特别是select操作。
PDO::exec()主要是针对没有结果集合返回的操作。如insert,update等操作。返回影响行数。
PDO::lastInsertId()返回上次插入操作最后一条ID,但要注意:如果用insert into tb(col1,col2) values(v1,v2),(v11,v22)..的方式一次插入多条记录,lastinsertid()返回的只是第一条(v1,v2)插入时的ID,而不是最后一条记录插入的记录ID。
PDOStatement::fetch()是用来获取一条记录。配合while来遍历。
PDOStatement::fetchAll()是获取所有记录集到一个中。
PDOStatement::fetchcolumn([int column_indexnum])用于直接访问列,参数column_indexnum是该列在行中的从0开始索引值,但是,这个方法一次只能取得同一行的一列,只要执行一次,就跳到下一行。因此,用于直接访问某一列时较好用,但要遍历多列就用不上。
PDOStatement::rowcount()适用于当用query(“select …”)方法时,获取记录的条数。也可以用于预处理中。$stmt->rowcount();
PDOStatement::columncount()适用于当用query(“select …”)方法时,获取记录的列数。

注解:
1、选fetch还是fetchall?
小记录集时,用fetchall效率高,减少从数据库检索次数,但对于大结果集,用fetchall则给系统带来很大负担。数据库要向WEB前端传输量太大反而效率低。
2、fetch()或fetchall()有几个参数:
mixed pdostatement::fetch([int fetch_style [,int cursor_orientation [,int cursor_offset]]])
array pdostatement::fetchAll(int fetch_style)

fetch_style参数:
■$row=$rs->fetchAll(PDO::FETCH_BOTH); FETCH_BOTH是默认的,可省,返回关联和索引。
■$row=$rs->fetchAll(PDO::FETCH_ASSOC); FETCH_ASSOC参数决定返回的只有关联数组。
■$row=$rs->fetchAll(PDO::FETCH_NUM); 返回索引数组
■$row=$rs->fetchAll(PDO::FETCH_OBJ); 如果fetch()则返回对象,如果是fetchall(),返回由对象组成的二维数组

实例:

<?php try{ $dbh=newPDO('mysql:host=localhost;dbname=test',$user,$pass); $dbh->setAttribute(PDO::ATTR_ERRMODE,PDO::ERRMODE_EXCEPTION); $dbh->exec("SET CHARACTER SET utf8"); /*添加*/ $sql = "INSERT INTO `user` (`login` ,`password`)VALUES (:login, :password)"; $stmt = $dbh->prepare($sql); $stmt->execute(array(':login'=>'king',':password'=>'123')); echo $dbh->lastinsertid(); /*修改*/ $sql = "UPDATE `user` SET `password`=:password WHERE `user_id`=:userId"; $stmt = $dbh->prepare($sql); $stmt->execute(array(':userId'=>'7', ':password'=>'4607e782c4d86fd5364d7e4508bb10d9')); echo $stmt->rowCount(); /*删除*/ $sql = "DELETE FROM `user` WHERE `login` LIKE 'ke'"; $stmt = $dbh->prepare($sql); $stmt->execute(); echo $stmt->rowCount(); /*查询*/ $login = 'kevi'; $sql = "SELECT * FROM `user` WHERE `login` LIKE :login"; $stmt = $dbh->prepare($sql); $stmt->execute(array(':login'=>$login)); while($row = $stmt->fetch(PDO::FETCH_ASSOC)){ print_r($row); } print_r( $stmt->fetchAll(PDO::FETCH_ASSOC)); $dbh=null; //断开连接 }catch(PDOException$e){ print"Error!:".$e->getMessage()."<br/>"; die(); } ?> 
Copy after login

事务:

<?php try { $dbh = new PDO('mysql:host=localhost;dbname=test', 'root', ''); $dbh->query('set names utf8;'); $dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); $dbh->beginTransaction(); $dbh->exec("Insert INTO `test`.`table` (`name` ,`age`)VALUES ('mick', 22);"); $dbh->exec("Insert INTO `test`.`table` (`name` ,`age`)VALUES ('lily', 29);"); $dbh->exec("Insert INTO `test`.`table` (`name` ,`age`)VALUES ('susan', 21);"); $dbh->commit(); } catch (Exception $e) { $dbh->rollBack(); echo "Failed: " . $e->getMessage(); } ?> 
Copy after login

排除错误:

$sth = $dbh->prepare('select * from db where SN = :SN'); $sth->bindParam(':SN',$value[SN]); $sth->execute(); if ($sth->errorCode()) { echo "有错误!有错误!"; print_r($sth->errorInfo()); } 
Copy after login

 

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!