本文阐述了PHP数据对象的PDO操作技巧。分享给大家参考,如下:
PHP数据对象(PDO)扩展为PHP访问数据库定义了一个轻量级且一致的接口。
?PHP try { $ DSN=' MySQL : host=localhost;port=3306dbname=wsq _ hotelcharset=utf-8 ';$ user=' root$ psw=' root$pdo=新PDO($dsn,$user,$ PSW);$sql='从wsq_goods_info中选择goods_prices,其中goods _ id=2 ';//$sql=“显示数据库”;$res=$pdo-query($sql)或var _ dump($ PDO-error info());//var _ dump($ RES);$ mon=$ RES-fetch(PDO : fetch _ ASSOC);echo $ mon[' goods _ price '];} catch(PDOException $ e){ echo $ e-Getmessage();}?PDO操作交易
//打开事务begin insulation()///roll back()//Commit Commit()//确定它是否在事务中间。返回最后插入的行的标识
Pdo: lastinstid () exec()执行
与查询()相比,exec()返回受影响的行数
$sql='插入到表值中'($ val ')';if(false===$ PDO-exec($ SQL)){ echo '执行失败';}PDO实现预编译
一种用于执行sql的语法,它引用预编译sql的结构
如果执行多个结构相同的sql,编译的中间结果(语法树)应该是一致的,这样就可以统一编译相同的结构,每次用不同的数据执行。
编制统一的结构
$ $ pdoStatement=$ PDO-prepare(SQL(SQL结构)将数据绑定到中间编译结果
$ pdostating-bind value()执行
$pdoStatement -execute()//$sql='插入到表值中(null,)';$sql='插入表值(null, name)';$ stmt=$ PDO-prepare($ SQL);//编译并执行多组数据//$stmt-bindValue(1,' bee ');$stmt-bindValue(':name ',' bee ');$ RES=$ stmt-execute();var _ dump($ RES);预编译可以更好的防止sql注入,因为不需要用户的数据参与预编译,所以编译时结构是固定的,所以数据不影响sql结构。
$pdo-query()和$pdo-execute()如果需要防止sql注入,可以使用$pdo-quote()(其功能是先转义后引用)
PDOstatement的常用方法:
error info()error code()fetchColumn()fetch()fetchAll()row count()close cursor()
Pdo应用
?phpheader(' content-type : text/html;charset=utf-8 ');类PDODB{静态私有$ _ init私有$ _ host private $ _ portprivate $ _ dbname private $ _用户名;私人$ _密码;私有$ _ charset私有$ _ dnsprivate $ _ pdo私有函数_ _ construct($ config){ $ this-_ initParamas($ config);$ this-_ initDNS();$ this-_ initDriverOptions();$ this-_ init PDO();}私有函数__clone(){}静态公共函数getInstance($config){ if(!static : $ _ init static的实例){ static : $ _ init=new static($ config);}返回static: $ _ init}私有函数_ initParamas($ config){ $ this-_ host=isset($ config[' host '])?$ config[' host ']: ' localhost ';$ this-_ port=isset($ config[' port '])?$ config[' port ']: ' 3306 ';$ this-_ dbname=isset($ config[' dbname '])?$ config[' db name ']: " ";$ this-_ username=isset($ config[' username '])?$ config[' username ']:“root”;$ this-_ passward=isset($ config[' passward '])?$ config[" passward "]: " ";$ this-_ charset=isset($ config[' charset '])?$ config[' charset ']: ' utf8 ';}私有函数_ init DNS(){ $ this-_ DNS=' MySQL : host=$ this-_ host;port=$ this-_ port;dbname=$ this-_ dbname ';} private function _ initDriverOptions(){ $ this-_ driverOptions=array(PDO : MySQL _ ATTR _ INIT _ COMMAND=' set name $ this-_ charset ');}私有函数_ init PDO(){ $ this-_ PDO=新PDO($ this-_ DNS,$ this-_用户名,$ this-_密码,$ this-_驱动程序)或骰子(‘失败’);}公共函数查询($sql){ if(!$ result=$ this-_ PDO-query($ SQL)){ $ erro=$ this-_ PDO-error info();"回声"失败的语句$sql .br ';"回声"错误代码$erro[1].br ';"回声"错误信息$erro[2].br ';死去;}返回$ result}公共函数fetchAll($ SQL){ $ RES=$ this-query($ SQL);$ list=$ RES-fetchAll(PDO : FETCH _ ASOC);$ RES-closeCursor();返回$ list}公共函数fetchRow($ SQL){ $ RES=$ this-query($ SQL);$ row=$ RES-fetch(PDO : fetch _ ASOC);$ RES-closeCursor();返回$ row}公共函数Fetchone($ SQL){ $ RES=$ this-query($ SQL);$ one=$ RES-FetchColumn();$ RES-closeCursor();返回一美元;} public function escape _ string($ data){ return $ this-_ PDO-quote($ data);} } $ config=array(' host '=' localhost ',' username'='root ',' passward'='root ',' dbname '=' students ');$ PDO=pdodb :3360 getinstance($ config);$sql='从学生中选择sdept,其中sage=21 ';var _ dump($ PDO-费特乔罗($ SQL));运行效果图如下:
更多关于服务器端编程语言(专业超文本预处理器的缩写)相关内容感兴趣的读者可查看本站专题: 《PHP基于pdo操作数据库技巧总结》 、 《php+Oracle数据库程序设计技巧总结》 、 《PHP+MongoDB数据库操作技巧大全》 、 《php面向对象程序设计入门教程》 、 《php字符串(string)用法总结》 、 《php+mysql数据库操作入门教程》 及《php常见数据库操作技巧汇总》
希望本文所述对大家服务器端编程语言(专业超文本预处理器的缩写)程序设计有所帮助。