标签:blog http java 使用 strong 文件
之前一段时间,开始了php的研究,看了关于PDO的一些资料,发现不错,整理和总结一下,作为开发笔记,留待日后使用,《PHP开发笔记系列(一)-PDO使用》。
PDO是PHP Data Objects的简称,是一种数据库访问抽象层。PDO是用于多种数据库的一致接口。类比的说,PDO做的事情类似于JAVA中的持久层框架(Hibernate、OpenJPA)的功能,为异构数据库提供一个统一的编程接口,这样就不必再使用mysql_*、pg_*这样的函数,也不必再写自己的"GenericDAO"了。PDO在PHP5.1的时候一起发布,所以我们用的PHP5.2、PHP5.3都已经可以使用。
为了方便,我们使用MySQL5来做演示。
0. 建立实验环境数据库及相关表
- CREATE TABLE `blog` (
- `id` int(10) NOT NULL AUTO_INCREMENT,
- `title` varchar(255) NOT NULL,
- PRIMARY KEY (`id`)
- ) ENGINE=InnoDB AUTO_INCREMENT=16 DEFAULT CHARSET=latin1
1. 使用PDO访问数据库
通过PDO访问数据库的步骤是:a)指定dsn、username、password,b)通过#a中的设置构造PDO对象,代码如下:
- file:pdo-access.php
- url:http:
- <?php
-
- $dsn = ‘mysql:host=localhost;dbname=pdotest‘;
- $username = ‘root‘;
- $passwd = ‘password‘;
-
-
- try {
- $dbh = new PDO($dsn, $username, $passwd);
- echo ‘connect to database successfully!‘;
- } catch (Exception $e) {
- echo ‘Fail to connect to database!\n‘;
- echo $e->getMessage();
- }
- ?>
备注:DSN即Data Source Name-数据源名称,提供数据库的连接信息,包括三部分,PDO驱动名称(MySQL、SQLite、PostgreSQL等)、冒号和驱动特定的语法。但是一般情况下,我们都很难记住这些,可以下载个php manual查,也可以到php的官网查。
2. 使用Query方法查询数据
在#1的基础上,连接数据库成功后,构造SQL语句,调用query方法返回结构数组,通过foreach进行数据结果遍历,代码如下:
- file:pdo-query.php
- url:http:
- <?php
-
- $dsn = ‘mysql:host=localhost;dbname=pdotest‘;
- $username = ‘root‘;
- $passwd = ‘password‘;
-
- try {
- $dbh = new PDO($dsn, $username, $passwd);
- echo ‘connect to database successfully!‘."\r\n";
-
- $title = ‘title1‘;
-
- $sql = "SELECT * FROM blog WHERE title = ‘".$title."‘";
-
- foreach ($dbh->query($sql) as $row){
- print $row[‘id‘]."\t";
- print $row[‘title‘]."\t";
- }
- } catch (PDOException $e) {
- echo ‘Errors occur when query data!\n‘;
- echo $e->getMessage();
- }
- ?>
备注:一般情况下, 通过构造SQL语句的方法来进行query、update、insert、delete,都会需要指定where条件,因此不可避免的需要防止SQL注入的问题出现。
例如,正常情况下,当用户输入“title1”时,我们构造的sql语句会是SELECT * FROM blog WHERE title=‘title1‘,但是对SQL比较熟悉的用户会输入‘OR id LIKE ‘%,此时我们构造的SQL就会变成SELECT * FROM blog where title=‘‘ OR id LIKE ‘%‘,这样整张blog 表中的数据都会被读取,因此需要避免,所以需要用到quote方法,把所有用户提供的数据进行转移,从而防止SQL注入的发生。使用quote方法后的sql为$sql = "SELECT * FROM blog WHERE title = ".$dbh->quote($title),转移出来后的sql是SELECT * FROM blog WHERE title = ‘\‘OR id LIKE \‘%‘,把所有的单引号(‘)都转移了。
3. 使用prepare和execute方法查询数据
如果我们用到的SQL查询是使用频率不高的查询,那么使用query或prepare和execute方法来查询都无太大差别,查询速度也不会差太远。两者不同的是,使用query时,php向数据库发送的sql,每执行一次都需要编译一次,而使用prepare和execute方法,则不需要,因此做大并发量的操作时,使用prepare和execute方法的优势会更加明显。
使用prepare和execute方法的步骤不多,a)构造SQL,b)将SQL传入PDO->prepart方法,得到一个PDOStatement对象,3)调用PDOStatement对象的execute方法,4)通过PDOStatement->fetch或PDOStatement->fetchObject遍历结果集。代码如下:
- file:pdo-prepare-fetch.php
- url:http:
- <?php
- $dsn = ‘mysql:host=localhost;dbname=pdotest‘;
- $username = ‘root‘;
- $passwd = ‘password‘;
-
-
- $title = $_GET[‘title‘];
- try {
- $dbh = new PDO($dsn, $username, $passwd);
- echo ‘connect to database successfully!‘."<br/>";
-
-
- $sql = "SELECT * FROM blog WHERE title = :title";
-
- $stmt = $dbh->prepare($sql);
-
- $stmt->bindParam(":title", $title, PDO::PARAM_STR);
-
- $stmt->execute();
-
- while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
- print $row[‘id‘]."\t";
- print $row[‘title‘]."\t";
- }
- } catch (PDOException $e) {
- echo ‘Errors occur when query data!\n‘;
- echo $e->getMessage();
- }
- ?>
除了使用上面的PDO::FETCH_ASSOC返回联合数组外,还可以使用fetchObject方法,返回结果集对象,代码如下:
- file:pdo-prepare-fetch-object.php
- url:http:
- <?php
- $dsn = ‘mysql:host=localhost;dbname=pdotest‘;
- $username = ‘root‘;
- $passwd = ‘password‘;
-
- $title = $_GET[‘title‘];
- try {
- $dbh = new PDO($dsn, $username, $passwd);
- echo ‘connect to database successfully!‘."<br/>";
-
- $sql = "SELECT * FROM blog WHERE title = :title";
- $stmt = $dbh->prepare($sql);
- $stmt->bindParam(":title", $title, PDO::PARAM_STR);
- $stmt->execute();
-
- while ($row = $stmt->fetchObject()) {
- print $row->id."\t";
- print $row->title."\t";
- }
- } catch (Exception $e) {
- echo ‘Errors occur when query data!\n‘;
- echo $e->getMessage();
- }
- ?>
4. 设置PDO的错误级别
PDO的错误级别分成PDO::ERRMODE_SILENT(默认)、PDO::ERRORMODE_WARNING、PDO::ERRORMODE_EXCEPTION三种。
PDO::ERRMODE_SILENT级别,当出现错误时,会自动设置PDOStatement对象的errorCode属性,但不进行任何其他操作,因此需要我们手工检查是否出现错误(使用empty($stmt->errorCode())),否则程序将继续走下去。
PDO::ERRORMODE_WARNING级别,基本与PDO::ERRMODE_SILENT一致,都是需要使用empty($stmt->errorCode())手工检查。
只需要在创建PDO对象后,加入以下代码即可:$dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_SILENT);或$dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_WARNING);
PDO::ERRORMODE_WARNING级别,当出现错误时,系统将抛出一个PDOException,并设置errorCode属性,程序可以通过try{...}catch{...}进行捕捉,否则未catch的exception会导致程序中断,加入以下代码即可:$dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
- <?php
- ...
- try {
- ...
- } catch (Exception $e) {
- echo ‘Errors occur when operation!‘."<br/>";
-
- echo $e->getMessage()."<br/>";
-
- echo $e->getCode()."<br/>";
-
- echo $e->getFile()."<br/>";
-
- echo $e->getLine()."<br/>";
-
- echo $e->getTraceAsString();
- }
- ?>
5. 使用prepare和execute方法插入/更新数据
方法和#3中进行查询的差不多,只是构造的SQL语句是insert语句或update语句,代码如下:
- file:pdo-prepare-insert.php
- url:http:
- <?php
- $dsn = ‘mysql:host=localhost;dbname=pdotest‘;
-
- $username = ‘root‘;
- $passwd = ‘password‘;
-
- $title = $_GET[‘title‘];
- try {
- $dbh = new PDO($dsn, $username, $passwd);
- $dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
- echo ‘connect to database successfully!‘."<br/>";
-
-
- $sql = "INSERT INTO blog(title) VALUES(:title)";
- $stmt = $dbh->prepare($sql);
- $stmt->bindParam(":title", $title);
- $stmt->execute();
- } catch (Exception $e) {
- echo ‘Errors occur when query data!\n‘;
- echo $e->getMessage();
- }
- ?>
- file:pdo-prepare-update.php
- url:http:
- <?php
- $dsn = ‘mysql:host=localhost;dbname=pdotest‘;
-
- $username = ‘root‘;
- $passwd = ‘password‘;
-
- $id = $_GET[‘id‘];
- $title = $_GET[‘title‘];
- try {
- $dbh = new PDO($dsn, $username, $passwd);
- $dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
- echo ‘connect to database successfully!‘."<br/>";
-
-
- $sql = "UPDATE blog SET title=:title where id=:id";
- $stmt = $dbh->prepare($sql);
- $stmt->bindParam(":id", $id);
- $stmt->bindParam(":title", $title);
- $stmt->execute();
- } catch (Exception $e) {
- echo ‘Errors occur when query data!\n‘;
- echo $e->getMessage();
- }
- ?>
6. 获取返回的行数
使用#3中的prepare和execute方法,然后将sql语句改成count的,例如SELECT COUNT(id) FROM article ...,代码如下:
- file:pdo-prepare-fetch-column.php
- url:http:
- <?php
- $dsn = ‘mysql:host=localhost;dbname=pdotest‘;
- $username = ‘root‘;
- $passwd = ‘password‘;
-
- try {
- $dbh = new PDO($dsn, $username, $passwd);
- $dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_SILENT);
- echo ‘connect to database successfully!‘."<br/>";
-
-
- $sql = "SELECT COUNT(id) FROM blog";
- $stmt = $dbh->prepare($sql);
- $stmt->execute();
-
- echo $stmt->fetchColumn()." rows returned!";
- } catch (Exception $e) {
- echo ‘Errors occur when query data!\n‘;
- echo $e->getMessage();
- }
- ?>
7. 获取受影响的行数
使用#3中的prepare和execute方法,然后将SQL语句改成insert、update、delete语句即可,代码如下:
- file:pdo-prepare-row-count.php
- url:http:
- <?php
- $dsn = ‘mysql:host=localhost;dbname=pdotest‘;
- $username = ‘root‘;
- $passwd = ‘password‘;
-
- $id = $_GET[‘id‘];
- try {
- $dbh = new PDO($dsn, $username, $passwd);
- $dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_SILENT);
- echo ‘connect to database successfully!‘."<br/>";
-
- $sql = "DELETE FROM blog WHERE id=:id";
- $stmt = $dbh->prepare($sql);
- $stmt->bindParam(":id", $id);
- $stmt->execute();
-
- echo $stmt->rowCount()." rows affected!";
- } catch (Exception $e) {
- echo ‘Errors occur when data operation!\n‘;
- echo $e->getMessage();
- }
- ?>
8. 获得新插入行的ID值
为数据库表插入新数据行时,我们需要获得刚刚插入的新行的ID值,此时我们需要使用到PDO的lastInsertId()方法,代码如下:
- file:pdo-prepare-last-insertid.php
- url:http:
- <?php
- $dsn = ‘mysql:host=localhost;dbname=pdotest‘;
-
- $username = ‘root‘;
- $passwd = ‘password‘;
-
- $title = $_GET[‘title‘];
- try {
- $dbh = new PDO($dsn, $username, $passwd);
- $dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
- echo ‘connect to database successfully!‘."<br/>";
-
- $sql = "INSERT INTO blog(title) VALUES(:title)";
- $stmt = $dbh->prepare($sql);
- $stmt->bindParam(":title", $title);
- $stmt->execute();
-
- echo $dbh->lastInsertId();
- } catch (Exception $e) {
- echo ‘Errors occur when query data!\n‘;
- echo $e->getMessage();
- }
- ?>
9. 使用PDO进行事务管理
事务是进行程序开发时,保证数据ACID(可分性、一致性、独立性、持久性)的工具。要不全部成功,要不全部不成功,这样才能保证关联数据的保存能够达到预期的目的。下面使用PDO的Transaction来进行实验,进行多比数据插入,开启事务,第一句sql是可以正常插入,第二句sql插入出错,检查是否rollback。
- file:pdo-prepare-transaction.php
- url:http:
- <?php
- $dsn = ‘mysql:host=localhost;dbname=pdotest‘;
-
- $username = ‘root‘;
- $passwd = ‘password‘;
-
- try {
- $dbh = new PDO($dsn, $username, $passwd);
- $dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
- echo ‘connect to database successfully!‘."<br/>";
-
- $dbh->beginTransaction();
- $sql = "INSERT INTO blog(title) VALUES(:title)";
- $stmt = $dbh->prepare($sql);
- $stmt->execute(array(‘:title‘=>‘insert title1‘));
- $stmt->execute(array(‘:title‘=>NULL));
-
- $dbh->commit();
- } catch (Exception $e) {
- echo ‘Errors occur when data operation!\n‘;
- echo $e->getMessage();
-
- $dbh->rollBack();
- }
- ?>
10. 使用PDO进行数据库备份
使用system函数,将我们构造的mysqldump命令传入即可。下面为了演示,只做了简单的调用。
- file:pdo-backup.php
- url:http:
- <?php
- $username="root";
- $passwd="password";
- $dbname="pdotest";
- $file=‘d:/‘.$dbname.‘.sql‘;
-
- $cmd = "mysqldump -u".$username." -p".$passwd." ".$dbname." >".$file;
-
- system($cmd,$error);
- if($error){
- trigger_error("backup failed".$error);
- }
- ?>
采用工厂模式:
- file:AbstractMySQLDump.php
- <?php
- require_once ‘MySQLDump_Win.php‘;
-
- abstract class AbstractMySQLDump {
- protected $cmd;
-
- abstract function __construct($username, $passwd, $dbname, $file);
-
-
- public static function factory($username, $passwd, $dbname, $file){
- if(strtoupper(substr(PHP_OS, 0, 3))===‘WIN‘){
- return new MySQLDump_Win($username, $passwd, $dbname, $file);
- }else{
- }
- }
-
-
- public function backup(){
- system($this->cmd, $error);
-
- if($error){
- trigger_error("backup failure! command:".$this->cmd." Error:".$error);
- }
- }
- }
- ?>
- file:MySQLDump_Win.php
- <?php
- class MySQLDump_Win extends AbstractMySQLDump {
-
-
- public function __construct($username, $passwd, $dbname, $file){
- $this->cmd = "mysqldump -u".$username." -p".$passwd." ".$dbname." > ".$file;
- }
- }
- ?>
- file:MySQLDumpTest.php
- url:http:
- <?php
- require_once ‘AbstractMySQLDump.php‘;
-
- $username = "root";
- $passwd = "password";
- $dbname = "pdotest";
- $file = "d:/".$dbname.".sql";
-
-
- $dump = AbstractMySQLDump::factory($username, $passwd, $dbname, $file);
-
- $dump->backup();
- ?>
PHP开发笔记系列(一)-PDO使用,布布扣,bubuko.com
PHP开发笔记系列(一)-PDO使用
标签:blog http java 使用 strong 文件
原文地址:http://www.cnblogs.com/smilesmile/p/3816686.html