使用PDO操作数据库的好处
Posted 蔡华锋
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了使用PDO操作数据库的好处相关的知识,希望对你有一定的参考价值。
PDO一是php数据对象(PHP Data Object)的缩写。
并不能使用PDO扩展本身执行任何数据库操作,必须使用一个database-specific PDO driver(针对特定数据库的PDO驱动)访问数据库服务器。
PDO并不提供数据库抽象,它并不会重写SQL或提供数据库本身缺失的功能,如果你需要这种功能,你需要使用一个更加成熟的抽象层。
PDO需要PHP5核心OO特性的支持,所以它无法运行于之前的PHP版本。
http://baike.baidu.com/view/1278977.htm
PDO有非常多的操作却是mysql扩展库所不具备的:
1:PDO真正的以底层实现的统一接口数库操作接口,不管后端使用的是何种数据库,如果代码封装好了以后,应用层调用基本上差不多的,当后端数据库更换了以后,应用层代码基本不用修改.
2:PDO支持更高级的DB特性操作,如:存储过程的调度等,mysql原生库是不支持的.
3:PDO是PHP官方的PECL库,兼容性稳定性必然要高于MySQL Extension,可以直接使用 pecl upgrade pdo 命令升级.
4:PDO可以防止SQL注入,确保数据库更加安全
PDO 防止SQL注入的原理
http://www.myhack58.com/Article/60/61/2015/63168.htm
预处理语句
使用语句预处理将帮助你免于SQL注入攻击。
一条预处理语句是一条预编译的 SQL 语句,它可以使用多次,每次只需将数据传至服务器。其额外优势在于可以对使用占位符的数据进行安全处理,防止SQL注入攻击。
http://www.php100.com/html/itnews/PHPxinwen/2013/0618/13555.html
5.根据PHP官方计划,PHP6正式到来之时,数据库链接方式统一为PDO。但是总有一小撮顽固分子,趁PHP官方还没正式统一时,还用老式的MYSQL驱动链接数据库。即使现在有部分程序改用Mysqli/pdo,只要没用到预编译,均和老式的Mysql驱动没多大区别。在此,我就不点评国内的PHP生态环境了。
回归主题,为什么说PHP必须要用PDO?除了官方要求之外,我认为作为PHP程序员,只要你目前是做开发的话,那么请选择用PDO的程序/框架!PDO除了安全和万金油式数据库链接,还有一点是我目前觉得非常好用的!下面我就用我最近的切身体会来说。
业务环境:公司某老架构,数据库设计的人员太菜了,设计过程完全没有按照数据库范式进行。各种表中使用大量的序列化形式保存(补充:json同理)。
出现问题:销售的客服反馈,网站某用户在编辑地址时,Mysql报错了。
问题猜想:不用说了。肯定是引号,反斜杠引起序列化入库不正常。
http://zhidao.baidu.com/question/1541430860923158627
6.安装配置及测试
在windows下进行有关pdo测试的时候,php.ini中的extension_dir的值要填为pdo*.dll的路径,否则无法运行pdo的相关程序。
; Directory in which the loadable extensions (modules) reside.
extension_dir = "E:\\www\\php5\\ext"
1 <?php 2 $host = \'localhost\'; 3 $user = \'root\'; 4 $password = \'develop\'; 5 $dbname = \'99game\'; 6 7 $dbh = new PDO("mysql:host=$host;dbname=$dbname", $user, $password); 8 9 //======================================================= 10 //例子 1. Execute a prepared statement with named placeholders 11 /* Execute a prepared statement by binding PHP variables */ 12 $user_id = 1; 13 $email = \'caihf_73940@qq.com\'; 14 $sth = $dbh->prepare(\'SELECT user_id,email,token FROM 99game_user 15 WHERE user_id = :user_id AND email = :email\'); 16 $sth->bindParam(\':user_id\', $user_id, PDO::PARAM_INT); 17 $sth->bindParam(\':email\', $email, PDO::PARAM_STR, 30); 18 $sth->execute(); 19 $result = $sth->fetch(PDO::FETCH_ASSOC); 20 print_r($result); 21 print("<br />\\n"); 22 23 //例子 2. Execute a prepared statement with question mark placeholders 24 /* Execute a prepared statement by binding PHP variables */ 25 $user_id = 2; 26 $email = \'caihuafeng1@gmail.com\'; 27 $sth = $dbh->prepare(\'SELECT user_id,email,token FROM 99game_user 28 WHERE user_id = ? AND email = ?\'); 29 $sth->bindParam(1, $user_id, PDO::PARAM_INT); 30 $sth->bindParam(2, $email, PDO::PARAM_STR, 30); 31 $sth->execute(); 32 $result = $sth->fetch(PDO::FETCH_ASSOC); 33 print_r($result); 34 print("<br />\\n"); 35 36 print "<hr />\\n"; 37 //======================================================= 38 39 //======================================================= 40 $sth = $dbh->prepare("SELECT user_id,email,token FROM 99game_user limit 10"); 41 $sth->execute(); 42 43 /* 运用 PDOStatement::fetch 风格 */ 44 print("PDO::FETCH_ASSOC: "); 45 print("Return next row as an array indexed by column name<br />\\n"); 46 $result = $sth->fetch(PDO::FETCH_ASSOC); 47 print_r($result); 48 print("<br />\\n"); 49 print("\\n"); 50 51 print("PDO::FETCH_BOTH: "); 52 print("Return next row as an array indexed by both column name and number<br />\\n"); 53 $result = $sth->fetch(PDO::FETCH_BOTH); 54 print_r($result); 55 print("<br />\\n"); 56 print("\\n"); 57 58 print("PDO::FETCH_LAZY: "); 59 print("Return next row as an anonymous object with column names as properties<br />\\n"); 60 $result = $sth->fetch(PDO::FETCH_LAZY); 61 print_r($result); 62 print("<br />\\n"); 63 print("\\n"); 64 65 print("PDO::FETCH_OBJ: "); 66 print("Return next row as an anonymous object with column names as properties<br />\\n"); 67 $result = $sth->fetch(PDO::FETCH_OBJ); 68 print_r($result); 69 print \'user_id:\' . $result->user_id; 70 print("<br />\\n"); 71 print("\\n"); 72 73 print "<hr />\\n"; 74 //======================================================= 75 76 //======================================================= 77 function readDataForwards($dbh) { 78 $sql = \'SELECT user_id,email,token FROM 99game_user limit 10\'; 79 try { 80 $stmt = $dbh->prepare($sql, array(PDO::ATTR_CURSOR, PDO::CURSOR_SCROLL)); 81 $stmt->execute(); 82 while ($row = $stmt->fetch(PDO::FETCH_NUM, PDO::FETCH_ORI_NEXT)) { 83 $data = $row[0] . "\\t" . $row[1] . "\\t" . $row[2] . "<br />\\n"; 84 print $data; 85 } 86 $stmt = null; 87 } 88 catch (PDOException $e) { 89 print $e->getMessage(); 90 } 91 } 92 93 function readDataBackwards($dbh) { 94 $sql = \'SELECT user_id,email,token FROM 99game_user limit 10\'; 95 try { 96 $stmt = $dbh->prepare($sql, array(PDO::ATTR_CURSOR => PDO::CURSOR_SCROLL)); 97 $stmt->execute(); 98 $row = $stmt->fetch(PDO::FETCH_NUM, PDO::FETCH_ORI_LAST); 99 do { 100 $data = $row[0] . "\\t" . $row[1] . "\\t" . $row[2] . "<br />\\n"; 101 print $data; 102 } while ($row = $stmt->fetch(PDO::FETCH_NUM, PDO::FETCH_ORI_PRIOR)); 103 $stmt = null; 104 } 105 catch (PDOException $e) { 106 print $e->getMessage(); 107 } 108 } 109 110 print "Reading forwards:<br />\\n"; 111 readDataForwards($dbh); 112 113 print "<hr />\\n"; 114 115 print "Reading backwards:<br />\\n"; 116 //下面的数据没有按照想像中的倒排输出,暂时不知道什么原因,php.net官方手册中的例子也是这么写的 117 readDataBackwards($dbh); 118 //======================================================= 119 ?>
以上测试程序输出如下:
Array
(
[user_id] => 1
[email] => caihf_73940@qq.com
[token] => 123token456_73940
)
Array
(
[user_id] => 2
[email] => caihuafeng1@gmail.com
[token] => 33fadfasdfadsf
)
PDO::FETCH_ASSOC: Return next row as an array indexed by column name
Array
(
[user_id] => 1
[email] => caihf_73940@qq.com
[token] => 123token456_73940
)
PDO::FETCH_BOTH: Return next row as an array indexed by both column name and number
Array
(
[user_id] => 2
[0] => 2
[email] => caihuafeng1@gmail.com
[1] => caihuafeng1@gmail.com
[token] => 33fadfasdfadsf
[2] => 33fadfasdfadsf
)
PDO::FETCH_LAZY: Return next row as an anonymous object with column names as properties
PDORow Object
(
[queryString] => SELECT user_id,email,token FROM 99game_user limit 10
[user_id] => 3
[email] => caihf_61039@qq.com
[token] => 123token456_61039
)
PDO::FETCH_OBJ: Return next row as an anonymous object with column names as properties
stdClass Object
(
[user_id] => 6
[email] => aa1@aa.com
[token] => cU8ady73epcmf54o7W0q1F0f8R3b2y4d
)
user_id:6
Reading forwards:
1 caihf_73940@qq.com 123token456_73940
2 caihuafeng1@gmail.com 33fadfasdfadsf
3 caihf_61039@qq.com 123token456_61039
6 aa1@aa.com cU8ady73epcmf54o7W0q1F0f8R3b2y4d
7 caihuafengqq@gmail.com 1wcl3j1a92bgc6eb8Y9rcQbjen5I8f7F
8 caihuafeng@163.com 90debU1x3ddWdSdc239hdF2t1AeScf3w
9 caihuafeng@gmail.com 1V1D8xao3O9m1mdq5706716kbx5u6n58
10 test@test.com b28de59ddc824ed8a8556514dd83c348
11 test2@test2.com 965e5873e6dbba4af57b96dfe0b027e7
12 caihf@qq.com_69054 123token456_69054
Reading backwards:
1 caihf_73940@qq.com 123token456_73940
2 caihuafeng1@gmail.com 33fadfasdfadsf
3 caihf_61039@qq.com 123token456_61039
6 aa1@aa.com cU8ady73epcmf54o7W0q1F0f8R3b2y4d
7 caihuafengqq@gmail.com 1wcl3j1a92bgc6eb8Y9rcQbjen5I8f7F
8 caihuafeng@163.com 90debU1x3ddWdSdc239hdF2t1AeScf3w
9 caihuafeng@gmail.com 1V1D8xao3O9m1mdq5706716kbx5u6n58
10 test@test.com b28de59ddc824ed8a8556514dd83c348
11 test2@test2.com 965e5873e6dbba4af57b96dfe0b027e7
12 caihf@qq.com_69054 123token456_69054
延伸阅读:
http://www.baidu.com/s?wd=php%20pdo%20%E4%BC%98%E7%82%B9
http://www.sogou.com/web?query=php%20pdo%20优点
https://www.so.com/s?q=php%20pdo%20%E4%BC%98%E7%82%B9
http://www.open-open.com/lib/view/open1419950418859.html
PHP5中PDO的简单使用
http://blog.csdn.net/leo115/article/details/7538983
php pdo操作数据库
http://www.cnblogs.com/yujon/p/5476176.html
为什么 PHP 应该使用 PDO 方式访问数据库
http://www.php100.com/html/itnews/PHPxinwen/2013/0618/13555.html
PDO预处理语句PDOStatement对象使用总结
http://www.jb51.net/article/57609.htm
以上是关于使用PDO操作数据库的好处的主要内容,如果未能解决你的问题,请参考以下文章
PHP中的PDO对象操作学习初始化PDO及原始SQL语句操作