| 订阅 | 在线投稿
分享
 
 
当前位置: 王朝网络 >> php >> PHP PDO xue xi bi ji

PHP PDO xue xi bi ji

2008-12-22 08:10:46 编辑來源:互联网 评论
 
 
 
本文为【PHP PDO 学习笔记】的拼音翻译版
  PHPPDO xue xi bi ji lib.cublog.cn 2007 nian 11 yue 13 ri 09:36 zuo zhe : liu shui meng chun 【 da zhong xiao 】
  ■PDO wei he wu ?
  POD(PHP Data Object) kuo zhan zai PHP5 zhong jia ru ,PHP6 zhong jiang mo ren shi yong PDO lian jie shu ju ku , suo you fei PDO kuo zhan jiang hui zai PHP6 bei cong kuo zhan zhong yi chu 。 gai kuo zhan ti gong PHP nei zhi lei PDO lai dui shu ju ku jin xing fang wen , bu tong shu ju ku shi yong xiang tong de fang fa ming , jie jue shu ju ku lian jie bu tong yi de wen ti 。
  wo shi pei zhi zai windows xia zuo kai fa yong de 。
  ■PDO de mu biao
  ti gong yi zhong qing xing 、 qing xi 、 fang bian de API tong yi ge zhong bu tong RDBMS ku de gong you te xing , dan bu pai chu geng gao ji de te xing 。 tong guo PHP jiao ben ti gong ke xuan de jiao da cheng du de chou xiang / jian rong xing 。
  ■PDO de te dian :
  xing neng 。PDO cong yi kai shi jiu xi qu le xian you shu ju ku kuo zhan cheng gong he shi bai de jing yan jiao xun 。 yin wei PDO de dai ma shi quan xin de , suo yi wo men you ji hui chong xin kai shi she ji xing neng , yi li yong PHP 5 de zui xin te xing 。 neng li 。PDO zhi zai jiang chang jian de shu ju ku gong neng zuo wei ji chu ti gong , tong shi ti gong dui yu RDBMS du te gong neng de fang bian fang wen 。 jian dan 。PDO zhi zai shi nin neng gou qing song shi yong shu ju ku 。API bu hui qiang xing jie ru nin de dai ma , tong shi hui qing chu di biao ming mei ge han shu tiao yong de guo cheng 。 yun xing shi ke kuo zhan 。PDO kuo zhan shi mo kuai hua de , shi nin neng gou zai yun xing shi wei nin de shu ju ku hou duan jia zai qu dong cheng xu , er bu bi chong xin bian yi huo chong xin an zhuang zheng ge PHP cheng xu 。 li ru ,PDO_OCI kuo zhan hui ti dai PDO kuo zhan shi xian Oracle shu ju ku API。 huan you yi xie yong yu MySQL、PostgreSQL、ODBC he Firebird de qu dong cheng xu , geng duo de qu dong cheng xu shang zai kai fa 。
  ■ an zhuang PDO
  wo zhe li shi WINDOWS xia kai fa yong de PDO kuo zhan , yao shi ni yao zai Linux xia an zhuang pei zhi , qing dao bie de di fang xun zhao 。
  ban ben yao qiu :
  php5.1 yi ji yi hou ban ben de cheng xu bao li yi jing dai le ;
  php5.0.x ze yao dao pecl.php.net xia zai , fang dao ni de kuo zhan ku , jiu shi PHP suo zai de wen jian jia de ext wen jian jia xia ;
  shou ce shang shuo 5.0 zhi qian de ban ben bu neng yun xing PDO kuo zhan 。
  pei zhi :
  xiu gai ni de php.ini pei zhi wen jian , shi ta zhi chi pdo.(php.ini zhe ge dong xi mei you nong dong de hua , xian nong qing chu , yao xiu gai tiao yong ni de phpinfo() han shu suo xian shi de na ge php.ini)
  ba
  extension=php_pdo.dll qian mian de fen hao qu diao , fen hao shi php pei zhi wen jian zhu shi fu hao , zhe ge kuo zhan shi bi xu de 。
  wang xia huan you
  ;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
  ge ge kuo zhan suo dui ying de shu ju ku shi :
  Driver nameSupported databasesPDO_DBLIBFreeTDS / Microsoft SQL Server / SybasePDO_FIREBIRDFirebird/Interbase 6PDO_INFORMIXIBM Informix Dynamic ServerPDO_MYSQLMySQL 3.x/4.xPDO_OCIOracle Call InterfacePDO_ODBCODBC v3 (IBM DB2, unixODBC and win32 ODBC)PDO_PGSQLPostgreSQLPDO_SQLITESQLite 3 and SQLite 2
  ni yao shi yong na zhong shu ju ku , zhi yao ba xiang ying de kuo zhan qian de zhu shi fu hao ";" qu diao jiu ke yi le 。
  ■ shi yong PDO
  wo zhe li jia she ni yi jing zhuang hao mysql le , yao shi mei zhuang de hua , ma fan xian xiang ban fa zhuang shang , wo de shi mysql5.0.22, hei ye lu ren yong de shi MySQL 4.0.26 ye ke yi yong 。
  ★ shu ju ku de lian jie :
  wo men tong guo xia mian de li zi lai fen xi PDO lian jie shu ju ku ,
  <?php
  $dbms='mysql'; // shu ju ku lei xing Oracle yong ODI, dui yu kai fa zhe lai shuo , shi yong bu tong de shu ju ku , zhi yao gai zhe ge , bu yong ji zhu na me duo de han shu le
  $host='localhost';// shu ju ku zhu ji ming
  $dbName='test'; // shi yong de shu ju ku
  $user='root'; // shu ju ku lian jie yong hu ming
  $pass=''; // dui ying de mi ma
  $dsn="$dbms:host=$host;dbname=$dbName";
  //
  try{
   $dbh=newPDO($dsn,$user,$pass);// chu shi hua yi ge PDO dui xiang , jiu shi chuang jian le shu ju ku lian jie dui xiang $dbh
   echo" lian jie cheng gong <br/>";
   /* ni huan ke yi jin xing yi ci sou suo cao zuo
   foreach($dbh->query('SELECT * from FOO')as$row){
   print_r($row);// ni ke yi yong echo($GLOBAL); lai kan dao zhe xie zhi
   }
   */
   $dbh=null;
  }catch(PDOException$e){
   die("Error!: ".$e->getMessage()."<br/>");
  }
  // mo ren zhe ge bu shi chang lian jie , ru guo xu yao shu ju ku chang lian jie , xu yao zui hou jia yi ge shen shu :array(PDO::ATTR_PERSISTENT => true) bian cheng zhe yang :
  $db=newPDO($dsn,$user,$pass,array(PDO::ATTR_PERSISTENT=>true));
  ?>
  ★ shu ju ku cha xun :
  shang mian wo men yi jing jin xing le yi ci cha xun , wo men huan ke yi shi yong ru xia de cha xun :
  <?php
  $db->setAttribute(PDO::ATTR_CASE,PDO::CASE_UPPER); // she zhi shu xing
  $rs=$db->query("SELECT * FROM foo");
  $rs->setFetchMode(PDO::FETCH_ASSOC);
  $result_arr=$rs->fetchAll();
  print_r($result_arr);
  ?>
  yi shang yin wei yong dao setAttribute() fang fa , fang shang na liang ge shen shu , ba zi duan ming qiang zhi zhuan huan cheng da xie 。 xia mian lie chu duo you PDO::setAttribute() de shen shu :
  PDO::ATTR_CASE: qiang zhi lie ming bian cheng yi zhong ge shi , xiang xi ru xia ( di er ge shen shu ):
  PDO::CASE_LOWER: qiang zhi lie ming shi xiao xie .
  PDO::CASE_NATURAL: lie ming an zhao yuan shi de fang shi
  PDO::CASE_UPPER: qiang zhi lie ming wei da xie .
  PDO::ATTR_ERRMODE: cuo wu ti shi .
  PDO::ERRMODE_SILENT: bu xian shi cuo wu xin xi , zhi xian shi cuo wu ma .
  PDO::ERRMODE_WARNING: xian shi jing gao cuo wu .
  PDO::ERRMODE_EXCEPTION: pao chu yi chang .
  PDO::ATTR_ORACLE_NULLS( bu jin jin shi ORACLE you xiao , bie de shu ju ku ye you xiao ): ) zhi ding shu ju ku fan hui de NULL zhi zai php zhong dui ying de shu zhi 。
  PDO::NULL_NATURAL: bu bian .
  PDO::NULL_EMPTY_STRING: Empty string is converted toNULL.
  PDO::NULL_TO_STRING: NULL is converted to an empty string.
  PDO::ATTR_STRINGIFY_FETCHES: Convert numeric values to strings when fetching. Requires bool.
  PDO::ATTR_STATEMENT_CLASS: Set user-supplied statement class derived from PDOStatement. Cannot be used with persistent PDO instances. Requiresarray(string classname, array(mixed constructor_args)).
  PDO::ATTR_AUTOCOMMIT(available in OCI, Firebird and MySQL): Whether to autocommit every single statement.
  PDO::MYSQL_ATTR_USE_BUFFERED_QUERY(available in MySQL): Use buffered queries.
  li zi zhong de $rs->setFetchMode(PDO::FETCH_ASSOC); shi PDOStatement::setFetchMode(), dui fan hui lei xing de sheng ming 。
  you ru xia :
  PDO::FETCH_ASSOC-- guan lian shu zu xing shi
  PDO::FETCH_NUM -- shu zi suo yin shu zu xing shi
  PDO::FETCH_BOTH -- liang zhe shu zu xing shi dou you , zhe shi que sheng de
  PDO::FETCH_OBJ -- an zhao dui xiang de xing shi , lei si yu yi qian de mysql_fetch_object()
  geng duo fan hui lei xing sheng ming (PDOStatement:: fang fa ming ) kan shou ce 。
  ★ cha ru , geng xin , shan chu shu ju ,
  $db->exec("DELETE FROM `xxxx_menu` where mid=43");
  jian dan de zong jie yi xia shang mian de cao zuo :
  cha xun cao zuo zhu yao shi PDO::query()、PDO::exec()、PDO::prepare()。
  PDO::query() zhu yao shi yong yu you ji lu jie guo fan hui de cao zuo , te bie shi SELECT cao zuo ,
  PDO::exec() zhu yao shi zhen dui mei you jie guo ji he fan hui de cao zuo , bi ru INSERT、UPDATE、DELETE deng cao zuo , ta fan hui de jie guo shi dang qian cao zuo ying xiang de lie shu 。
  PDO::prepare() zhu yao shi yu chu li cao zuo , xu yao tong guo $rs->execute() lai zhi xing yu chu li li mian de SQL yu ju , zhe ge fang fa ke yi bang ding shen shu , gong neng bi jiao qiang da , bu shi ben wen neng gou jian dan shuo ming bai de , da jia ke yi shen kao shou ce he qi ta wen dang 。
  huo qu jie guo ji cao zuo zhu yao shi :PDOStatement::fetchColumn()、PDOStatement::fetch()、PDOStatement::fetchALL()。
  PDOStatement::fetchColumn() shi huo qu jie guo zhi ding di yi tiao ji lu de mou ge zi duan , que sheng shi di yi ge zi duan 。
  PDOStatement::fetch() shi yong lai huo qu yi tiao ji lu ,
  PDOStatement::fetchAll() shi huo qu suo you ji lu ji dao yi ge zhong , huo qu jie guo ke yi tong guo PDOStatement::setFetchMode lai she zhi xu yao jie guo ji he de lei xing 。
  ling wai you liang ge zhou bian de cao zuo , yi ge shi PDO::lastInsertId() he PDOStatement::rowCount()。PDO::lastInsertId() shi fan hui shang ci cha ru cao zuo , zhu jian lie lei xing shi zi zeng de zui hou de zi zeng ID。
  PDOStatement::rowCount() zhu yao shi yong yu PDO::query() he PDO::prepare() jin xing DELETE、INSERT、UPDATE cao zuo ying xiang de jie guo ji , dui PDO::exec() fang fa he SELECT cao zuo wu xiao 。
  ★ shi wu he zi dong ti jiao
   zhi ci , nin yi jing tong guo PDO lian jie dao le mysql, zai fa chu cha xun zhi qian , nin ying gai li jie PDO shi ru he guan li shi wu de 。 ru guo zhi qian mei you jie chu guo shi wu , na me shou xian yao zhi dao shi wu de 4 ge te zheng : yuan zi xing (Atomicity)、 yi zhi xing (Consistency)、 du li xing (Isolation) he chi jiu xing (Durability), ji ACID。 yong wai xing ren de hua shuo , dui yu zai yi ge shi wu zhong zhi xing de ren he gong zuo , ji shi ta shi fen jie duan zhi xing de , ye yi ding ke yi bao zheng gai gong zuo hui an quan di ying yong yu shu ju ku , bing qie zai gong zuo bei ti jiao shi , bu hui shou dao lai zi qi ta lian jie de ying xiang 。 shi wu xing gong zuo ke yi gen ju qing qiu zi dong che xiao ( jia she nin huan mei you ti jiao ta ), zhe shi de jiao ben zhong de cuo wu chu li bian de geng jia rong yi 。
   shi wu tong chang shi tong guo ba yi pi geng gai ji xu qi lai 、 shi zhi tong shi sheng xiao er shi xian de 。 zhe yang zuo de hao chu shi ke yi da da ti gao zhe xie geng xin de xiao lv 。 huan ju hua shuo , shi wu ke yi shi jiao ben geng kuai , er qie ke neng geng jian zhuang ( bu guo xu yao zheng que di shi yong shi wu cai neng huo de zhe yang de hao chu )。
   bu xing de shi , bing bu shi mei zhong shu ju ku dou zhi chi shi wu (Mysql5 zhi chi shi wu ,mysql4 wo bu zhi dao ), suo yi dang di yi ci da kai lian jie shi ,PDO xu yao zai suo wei de “ zi dong ti jiao (auto-commit)” mo shi xia yun xing 。 zi dong ti jiao mo shi yi wei zhe , ru guo shu ju ku zhi chi shi wu , na me nin suo yun xing de mei yi ge cha xun dou you ta zi ji de yin shi shi wu , ru guo shu ju ku bu zhi chi shi wu , mei ge cha xun jiu mei you zhe yang de shi wu 。 ru guo nin xu yao yi ge shi wu , na me bi xu shi yong PDO::beginTransaction() fang fa lai qi dong yi ge shi wu 。 ru guo di ceng qu dong cheng xu bu zhi chi shi wu , na me jiang hui pao chu yi ge PDOException( wu lun cuo wu chu li she zhi shi zen yang de : zhe zong shi yi ge yan chong cuo wu zhuang tai )。 zai yi ge shi wu zhong , ke yi shi yong PDO::commit() huo PDO::rollBack() lai jie shu gai shi wu , zhe qu jue yu shi wu zhong yun xing de dai ma shi fou cheng gong 。
   dang jiao ben jie shu shi , huo zhe dang yi ge lian jie ji jiang bei guan bi shi , ru guo you yi ge wei wan cheng de shi wu , na me PDO jiang zi dong hui gun gai shi wu 。 zhe shi yi zhong an quan cuo shi , you zhu yu bi mian zai jiao ben fei zheng chang jie shu shi chu xian bu yi zhi de qing kuang —— ru guo mei you xian shi di ti jiao shi wu , na me jia she you mou ge di fang hui chu xian bu yi zhi , suo yi yao zhi xing hui gun , yi bao zheng shu ju de an quan xing 。
  // li zi lai zi http://www.ibm.com/developerworks/cn/db2/library/techarticles/dm-0505furlong/index.html
  try{
  $dbh=new PDO('odbc:SAMPLE','db2inst1','ibmdb2',
   array(PDO_ATTR_PERSISTENT=>true));
  echo"Connected\n";
  $dbh->setAttribute(PDO_ATTR_ERRMODE,PDO_ERRMODE_EXCEPTION);
  $dbh->beginTransaction();
  $dbh->exec("insert into staff (id, first, last) values (23, 'Joe', 'Bloggs')");
  $dbh->exec("insert into salarychange (id, amount, changedate)
   values (23, 50000, NOW())");
  $dbh->commit();
  }catch(Exception $e){
  $dbh->rollBack();
  echo"Failed: ".$e->getMessage();
  }
  zai shang mian de shi li zhong , jia she wo men wei yi ge xin gu yuan chuang jian yi zu tiao mu , zhe ge gu yuan you yi ge ID hao , ji 23。 chu le shu ru zhe ge ren de ji ben shu ju wai , wo men huan xu yao ji lu gu yuan de xin shui 。 liang ge geng xin fen bie wan cheng qi lai hen jian dan , dan tong guo jiang zhe liang ge geng xin bao kuo zai beginTransaction() he commit() tiao yong zhong , jiu ke yi bao zheng zai geng gai wan cheng zhi qian , qi ta ren wu fa kan dao geng gai 。 ru guo fa sheng le cuo wu ,catch kuai ke yi hui gun shi wu kai shi yi lai fa sheng de suo you geng gai , bing da yin chu yi tiao cuo wu xiao xi 。
  bing bu shi yi ding yao zai shi wu zhong zuo chu geng xin 。 nin ye ke yi fa chu fu duo de cha xun lai ti qu shu ju , huan ke yi shi yong na zhong xin xi gou jian geng duo de geng xin he cha xun 。 dang shi wu zai huo dong shi , ke yi bao zheng qi ta ren zai gong zuo jin xing dang zhong wu fa zuo chu geng gai 。 shi shi shang , zhe bu shi 100% de zheng que , dan ru guo nin zhi qian mei you ting shuo guo shi wu de hua , zhe yang jie shao ye wei chang bu ke 。
  ★ yu chu li yu ju he cun chu guo cheng
  hen duo geng cheng shu de shu ju ku dou zhi chi yu chu li yu ju de gai nian 。 shen me shi yu chu li yu ju ? nin ke yi ba yu chu li yu ju kan zuo nin xiang yao yun xing de SQL de yi zhong bian yi guo de mo ban , ta ke yi shi yong bian liang shen shu jin xing ding zhi 。 yu chu li yu ju ke yi dai lai liang da hao chu :
  cha xun zhi xu jie xi ( huo zhun bei ) yi ci , dan shi ke yi yong xiang tong huo bu tong de shen shu zhi xing duo ci 。 dang cha xun zhun bei hao hou , shu ju ku jiang fen xi 、 bian yi he you hua zhi xing gai cha xun de ji hua 。 dui yu fu duo de cha xun , zhe ge guo cheng yao hua bi jiao chang de shi jian , ru guo nin xu yao yi bu tong shen shu duo ci chong fu xiang tong de cha xun , na me gai guo cheng jiang da da jiang di ying yong cheng xu de su du 。 tong guo shi yong yu chu li yu ju , ke yi bi mian chong fu fen xi / bian yi / you hua zhou qi 。 jian yan zhi , yu chu li yu ju shi yong geng shao de zi yuan , yin er yun xing de geng kuai 。
  ti gong gei yu chu li yu ju de shen shu bu xu yao yong yin hao kuo qi lai , qu dong cheng xu hui chu li zhe xie 。 ru guo ying yong cheng xu du zhan di shi yong yu chu li yu ju , na me ke yi que bao mei you SQL ru qin fa sheng 。( ran er , ru guo nin reng ran jiang cha xun de qi ta bu fen jian li zai bu shou xin ren de shu ru zhi shang , na me jiu reng ran cun zai feng xian )。
  yu chu li yu ju shi ru ci you yong , yi zhi PDO shi ji shang da po le zai mu biao 4 zhong she xia de gui ze : ru guo qu dong cheng xu bu zhi chi yu chu li yu ju , na me PDO jiang fang zhen yu chu li yu ju 。
  shi li :PDO de ying yong li zi :
  <?php
  $dbms='mysql';// shu ju ku lei xing Oracle yong ODI, dui yu kai fa zhe lai shuo , shi yong bu tong de shu ju ku , zhi yao gai zhe ge , bu yong ji zhu na me duo de han shu le
  $host='localhost';// shu ju ku zhu ji ming
  $dbName='test';// shi yong de shu ju ku
  $user='root';// shu ju ku lian jie yong hu ming
  $pass='';// dui ying de mi ma
  $dsn="$dbms:host=$host;dbname=$dbName";
  classdbextendsPDO{
   publicfunction__construct(){
   try{
   parent::__construct("$GLOBALS[dsn]",$GLOBALS['user'],$GLOBALS['pass']);
   }catch(PDOException$e){
   die("Error: ".$e->__toString()."<br/>");
   }
   }
  
   publicfinalfunctionquery($sql){
   try{
   returnparent::query($this->setString($sql));
   }catch(PDOException$e){
   die("Error: ".$e->__toString()."<br/>");
   }
   }
  
   privatefinalfunctionsetString($sql){
   echo" wo yao chu li yi xia $sql";
   return$sql;
   }
  }
  $db=newdb();
  $db->setAttribute(PDO::ATTR_CASE,PDO::CASE_UPPER);
  foreach($db->query('SELECT * from xxxx_menu')as$row){
   print_r($row);
  }
  $db->exec('DELETE FROM `xxxx_menu` where mid=43');
  ?>【原文】【汉音对照
 
 
 
 
 
 
 
 
 
日版宠物情人插曲《Winding Road》歌词

日版宠物情人2017的插曲,很带节奏感,日语的,女生唱的。 最后听见是在第8集的时候女主手割伤了,然后男主用嘴帮她吸了一下,插曲就出来了。 歌手:Def...

兄弟共妻,我成了他们夜里的美食

老钟家的两个儿子很特别,就是跟其他的人不太一样,魔一般的执着。兄弟俩都到了要结婚的年龄了,不管自家老爹怎么磨破嘴皮子,兄弟俩说不娶就不娶,老父母为兄弟两操碎了心...

如何磨出破洞牛仔裤?牛仔裤怎么剪破洞?

把牛仔裤磨出有线的破洞 1、具体工具就是磨脚石,下面垫一个硬物,然后用磨脚石一直磨一直磨,到把那块磨薄了,用手撕开就好了。出来的洞啊很自然的。需要猫须的话调几...

我就是扫描下图得到了敬业福和爱国福

先来看下敬业福和爱国福 今年春节,支付宝再次推出了“五福红包”活动,表示要“把欠大家的敬业福都还给大家”。 今天该活动正式启动,和去年一样,需要收集“五福”...

冰箱异味产生的原因和臭味去除的方法

有时候我们打开冰箱就会闻到一股异味,冰箱里的这种异味是因为一些物质发出的气味的混合体,闻起来让人恶心。 产生这些异味的主要原因有以下几点。 1、很多人有这种习...

 
 
PHPPDO xue xi bi ji lib.cublog.cn 2007 nian 11 yue 13 ri 09:36 zuo zhe : liu shui meng chun 【 da zhong xiao 】 ■PDO wei he wu ? POD(PHP Data Object) kuo zhan zai PHP5 zhong jia ru ,PHP6 zhong jiang mo ren shi yong PDO lian jie shu ju ku , suo you fei PDO kuo zhan jiang hui zai PHP6 bei cong kuo zhan zhong yi chu 。 gai kuo zhan ti gong PHP nei zhi lei PDO lai dui shu ju ku jin xing fang wen , bu tong shu ju ku shi yong xiang tong de fang fa ming , jie jue shu ju ku lian jie bu tong yi de wen ti 。 wo shi pei zhi zai windows xia zuo kai fa yong de 。 ■PDO de mu biao ti gong yi zhong qing xing 、 qing xi 、 fang bian de API tong yi ge zhong bu tong RDBMS ku de gong you te xing , dan bu pai chu geng gao ji de te xing 。 tong guo PHP jiao ben ti gong ke xuan de jiao da cheng du de chou xiang / jian rong xing 。 ■PDO de te dian : xing neng 。PDO cong yi kai shi jiu xi qu le xian you shu ju ku kuo zhan cheng gong he shi bai de jing yan jiao xun 。 yin wei PDO de dai ma shi quan xin de , suo yi wo men you ji hui chong xin kai shi she ji xing neng , yi li yong PHP 5 de zui xin te xing 。 neng li 。PDO zhi zai jiang chang jian de shu ju ku gong neng zuo wei ji chu ti gong , tong shi ti gong dui yu RDBMS du te gong neng de fang bian fang wen 。 jian dan 。PDO zhi zai shi nin neng gou qing song shi yong shu ju ku 。API bu hui qiang xing jie ru nin de dai ma , tong shi hui qing chu di biao ming mei ge han shu tiao yong de guo cheng 。 yun xing shi ke kuo zhan 。PDO kuo zhan shi mo kuai hua de , shi nin neng gou zai yun xing shi wei nin de shu ju ku hou duan jia zai qu dong cheng xu , er bu bi chong xin bian yi huo chong xin an zhuang zheng ge PHP cheng xu 。 li ru ,PDO_OCI kuo zhan hui ti dai PDO kuo zhan shi xian Oracle shu ju ku API。 huan you yi xie yong yu MySQL、PostgreSQL、ODBC he Firebird de qu dong cheng xu , geng duo de qu dong cheng xu shang zai kai fa 。 ■ an zhuang PDO wo zhe li shi WINDOWS xia kai fa yong de PDO kuo zhan , yao shi ni yao zai Linux xia an zhuang pei zhi , qing dao bie de di fang xun zhao 。 ban ben yao qiu : php5.1 yi ji yi hou ban ben de cheng xu bao li yi jing dai le ; php5.0.x ze yao dao pecl.php.net xia zai , fang dao ni de kuo zhan ku , jiu shi PHP suo zai de wen jian jia de ext wen jian jia xia ; shou ce shang shuo 5.0 zhi qian de ban ben bu neng yun xing PDO kuo zhan 。 pei zhi : xiu gai ni de php.ini pei zhi wen jian , shi ta zhi chi pdo.(php.ini zhe ge dong xi mei you nong dong de hua , xian nong qing chu , yao xiu gai tiao yong ni de phpinfo() han shu suo xian shi de na ge php.ini) ba extension=php_pdo.dll qian mian de fen hao qu diao , fen hao shi php pei zhi wen jian zhu shi fu hao , zhe ge kuo zhan shi bi xu de 。 wang xia huan you ;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 ge ge kuo zhan suo dui ying de shu ju ku shi : Driver nameSupported databasesPDO_DBLIBFreeTDS / Microsoft SQL Server / SybasePDO_FIREBIRDFirebird/Interbase 6PDO_INFORMIXIBM Informix Dynamic ServerPDO_MYSQLMySQL 3.x/4.xPDO_OCIOracle Call InterfacePDO_ODBCODBC v3 (IBM DB2, unixODBC and win32 ODBC)PDO_PGSQLPostgreSQLPDO_SQLITESQLite 3 and SQLite 2 ni yao shi yong na zhong shu ju ku , zhi yao ba xiang ying de kuo zhan qian de zhu shi fu hao ";" qu diao jiu ke yi le 。 ■ shi yong PDO wo zhe li jia she ni yi jing zhuang hao mysql le , yao shi mei zhuang de hua , ma fan xian xiang ban fa zhuang shang , wo de shi mysql5.0.22, hei ye lu ren yong de shi MySQL 4.0.26 ye ke yi yong 。 ★ shu ju ku de lian jie : wo men tong guo xia mian de li zi lai fen xi PDO lian jie shu ju ku , <?php $dbms='mysql'; // shu ju ku lei xing Oracle yong ODI, dui yu kai fa zhe lai shuo , shi yong bu tong de shu ju ku , zhi yao gai zhe ge , bu yong ji zhu na me duo de han shu le $host='localhost';// shu ju ku zhu ji ming $dbName='test'; // shi yong de shu ju ku $user='root'; // shu ju ku lian jie yong hu ming $pass=''; // dui ying de mi ma $dsn="$dbms:host=$host;dbname=$dbName"; // try{ $dbh=newPDO($dsn,$user,$pass);// chu shi hua yi ge PDO dui xiang , jiu shi chuang jian le shu ju ku lian jie dui xiang $dbh echo" lian jie cheng gong <br/>"; /* ni huan ke yi jin xing yi ci sou suo cao zuo foreach($dbh->query('SELECT * from FOO')as$row){ print_r($row);// ni ke yi yong echo($GLOBAL); lai kan dao zhe xie zhi } */ $dbh=null; }catch(PDOException$e){ die("Error!: ".$e->getMessage()."<br/>"); } // mo ren zhe ge bu shi chang lian jie , ru guo xu yao shu ju ku chang lian jie , xu yao zui hou jia yi ge shen shu :array(PDO::ATTR_PERSISTENT => true) bian cheng zhe yang : $db=newPDO($dsn,$user,$pass,array(PDO::ATTR_PERSISTENT=>true)); ?> ★ shu ju ku cha xun : shang mian wo men yi jing jin xing le yi ci cha xun , wo men huan ke yi shi yong ru xia de cha xun : <?php $db->setAttribute(PDO::ATTR_CASE,PDO::CASE_UPPER); // she zhi shu xing $rs=$db->query("SELECT * FROM foo"); $rs->setFetchMode(PDO::FETCH_ASSOC); $result_arr=$rs->fetchAll(); print_r($result_arr); ?> yi shang yin wei yong dao setAttribute() fang fa , fang shang na liang ge shen shu , ba zi duan ming qiang zhi zhuan huan cheng da xie 。 xia mian lie chu duo you PDO::setAttribute() de shen shu : PDO::ATTR_CASE: qiang zhi lie ming bian cheng yi zhong ge shi , xiang xi ru xia ( di er ge shen shu ): PDO::CASE_LOWER: qiang zhi lie ming shi xiao xie . PDO::CASE_NATURAL: lie ming an zhao yuan shi de fang shi PDO::CASE_UPPER: qiang zhi lie ming wei da xie . PDO::ATTR_ERRMODE: cuo wu ti shi . PDO::ERRMODE_SILENT: bu xian shi cuo wu xin xi , zhi xian shi cuo wu ma . PDO::ERRMODE_WARNING: xian shi jing gao cuo wu . PDO::ERRMODE_EXCEPTION: pao chu yi chang . PDO::ATTR_ORACLE_NULLS( bu jin jin shi ORACLE you xiao , bie de shu ju ku ye you xiao ): ) zhi ding shu ju ku fan hui de NULL zhi zai php zhong dui ying de shu zhi 。 PDO::NULL_NATURAL: bu bian . PDO::NULL_EMPTY_STRING: Empty string is converted toNULL. PDO::NULL_TO_STRING: NULL is converted to an empty string. PDO::ATTR_STRINGIFY_FETCHES: Convert numeric values to strings when fetching. Requires bool. PDO::ATTR_STATEMENT_CLASS: Set user-supplied statement class derived from PDOStatement. Cannot be used with persistent PDO instances. Requiresarray(string classname, array(mixed constructor_args)). PDO::ATTR_AUTOCOMMIT(available in OCI, Firebird and MySQL): Whether to autocommit every single statement. PDO::MYSQL_ATTR_USE_BUFFERED_QUERY(available in MySQL): Use buffered queries. li zi zhong de $rs->setFetchMode(PDO::FETCH_ASSOC); shi PDOStatement::setFetchMode(), dui fan hui lei xing de sheng ming 。 you ru xia : PDO::FETCH_ASSOC-- guan lian shu zu xing shi PDO::FETCH_NUM -- shu zi suo yin shu zu xing shi PDO::FETCH_BOTH -- liang zhe shu zu xing shi dou you , zhe shi que sheng de PDO::FETCH_OBJ -- an zhao dui xiang de xing shi , lei si yu yi qian de mysql_fetch_object() geng duo fan hui lei xing sheng ming (PDOStatement:: fang fa ming ) kan shou ce 。 ★ cha ru , geng xin , shan chu shu ju , $db->exec("DELETE FROM `xxxx_menu` where mid=43"); jian dan de zong jie yi xia shang mian de cao zuo : cha xun cao zuo zhu yao shi PDO::query()、PDO::exec()、PDO::prepare()。 PDO::query() zhu yao shi yong yu you ji lu jie guo fan hui de cao zuo , te bie shi SELECT cao zuo , PDO::exec() zhu yao shi zhen dui mei you jie guo ji he fan hui de cao zuo , bi ru INSERT、UPDATE、DELETE deng cao zuo , ta fan hui de jie guo shi dang qian cao zuo ying xiang de lie shu 。 PDO::prepare() zhu yao shi yu chu li cao zuo , xu yao tong guo $rs->execute() lai zhi xing yu chu li li mian de SQL yu ju , zhe ge fang fa ke yi bang ding shen shu , gong neng bi jiao qiang da , bu shi ben wen neng gou jian dan shuo ming bai de , da jia ke yi shen kao shou ce he qi ta wen dang 。 huo qu jie guo ji cao zuo zhu yao shi :PDOStatement::fetchColumn()、PDOStatement::fetch()、PDOStatement::fetchALL()。 PDOStatement::fetchColumn() shi huo qu jie guo zhi ding di yi tiao ji lu de mou ge zi duan , que sheng shi di yi ge zi duan 。 PDOStatement::fetch() shi yong lai huo qu yi tiao ji lu , PDOStatement::fetchAll() shi huo qu suo you ji lu ji dao yi ge zhong , huo qu jie guo ke yi tong guo PDOStatement::setFetchMode lai she zhi xu yao jie guo ji he de lei xing 。 ling wai you liang ge zhou bian de cao zuo , yi ge shi PDO::lastInsertId() he PDOStatement::rowCount()。PDO::lastInsertId() shi fan hui shang ci cha ru cao zuo , zhu jian lie lei xing shi zi zeng de zui hou de zi zeng ID。 PDOStatement::rowCount() zhu yao shi yong yu PDO::query() he PDO::prepare() jin xing DELETE、INSERT、UPDATE cao zuo ying xiang de jie guo ji , dui PDO::exec() fang fa he SELECT cao zuo wu xiao 。 ★ shi wu he zi dong ti jiao zhi ci , nin yi jing tong guo PDO lian jie dao le mysql, zai fa chu cha xun zhi qian , nin ying gai li jie PDO shi ru he guan li shi wu de 。 ru guo zhi qian mei you jie chu guo shi wu , na me shou xian yao zhi dao shi wu de 4 ge te zheng : yuan zi xing (Atomicity)、 yi zhi xing (Consistency)、 du li xing (Isolation) he chi jiu xing (Durability), ji ACID。 yong wai xing ren de hua shuo , dui yu zai yi ge shi wu zhong zhi xing de ren he gong zuo , ji shi ta shi fen jie duan zhi xing de , ye yi ding ke yi bao zheng gai gong zuo hui an quan di ying yong yu shu ju ku , bing qie zai gong zuo bei ti jiao shi , bu hui shou dao lai zi qi ta lian jie de ying xiang 。 shi wu xing gong zuo ke yi gen ju qing qiu zi dong che xiao ( jia she nin huan mei you ti jiao ta ), zhe shi de jiao ben zhong de cuo wu chu li bian de geng jia rong yi 。 shi wu tong chang shi tong guo ba yi pi geng gai ji xu qi lai 、 shi zhi tong shi sheng xiao er shi xian de 。 zhe yang zuo de hao chu shi ke yi da da ti gao zhe xie geng xin de xiao lv 。 huan ju hua shuo , shi wu ke yi shi jiao ben geng kuai , er qie ke neng geng jian zhuang ( bu guo xu yao zheng que di shi yong shi wu cai neng huo de zhe yang de hao chu )。 bu xing de shi , bing bu shi mei zhong shu ju ku dou zhi chi shi wu (Mysql5 zhi chi shi wu ,mysql4 wo bu zhi dao ), suo yi dang di yi ci da kai lian jie shi ,PDO xu yao zai suo wei de “ zi dong ti jiao (auto-commit)” mo shi xia yun xing 。 zi dong ti jiao mo shi yi wei zhe , ru guo shu ju ku zhi chi shi wu , na me nin suo yun xing de mei yi ge cha xun dou you ta zi ji de yin shi shi wu , ru guo shu ju ku bu zhi chi shi wu , mei ge cha xun jiu mei you zhe yang de shi wu 。 ru guo nin xu yao yi ge shi wu , na me bi xu shi yong PDO::beginTransaction() fang fa lai qi dong yi ge shi wu 。 ru guo di ceng qu dong cheng xu bu zhi chi shi wu , na me jiang hui pao chu yi ge PDOException( wu lun cuo wu chu li she zhi shi zen yang de : zhe zong shi yi ge yan chong cuo wu zhuang tai )。 zai yi ge shi wu zhong , ke yi shi yong PDO::commit() huo PDO::rollBack() lai jie shu gai shi wu , zhe qu jue yu shi wu zhong yun xing de dai ma shi fou cheng gong 。 dang jiao ben jie shu shi , huo zhe dang yi ge lian jie ji jiang bei guan bi shi , ru guo you yi ge wei wan cheng de shi wu , na me PDO jiang zi dong hui gun gai shi wu 。 zhe shi yi zhong an quan cuo shi , you zhu yu bi mian zai jiao ben fei zheng chang jie shu shi chu xian bu yi zhi de qing kuang —— ru guo mei you xian shi di ti jiao shi wu , na me jia she you mou ge di fang hui chu xian bu yi zhi , suo yi yao zhi xing hui gun , yi bao zheng shu ju de an quan xing 。 // li zi lai zi [url=http://www.ibm.com/developerworks/cn/db2/library/techarticles/dm-0505furlong/index.html]http://www.ibm.com/developerworks/cn/db2/library/techarticles/dm-0505furlong/index.html[/url] try{ $dbh=new PDO('odbc:SAMPLE','db2inst1','ibmdb2', array(PDO_ATTR_PERSISTENT=>true)); echo"Connected\n"; $dbh->setAttribute(PDO_ATTR_ERRMODE,PDO_ERRMODE_EXCEPTION); $dbh->beginTransaction(); $dbh->exec("insert into staff (id, first, last) values (23, 'Joe', 'Bloggs')"); $dbh->exec("insert into salarychange (id, amount, changedate) values (23, 50000, NOW())"); $dbh->commit(); }catch(Exception $e){ $dbh->rollBack(); echo"Failed: ".$e->getMessage(); } zai shang mian de shi li zhong , jia she wo men wei yi ge xin gu yuan chuang jian yi zu tiao mu , zhe ge gu yuan you yi ge ID hao , ji 23。 chu le shu ru zhe ge ren de ji ben shu ju wai , wo men huan xu yao ji lu gu yuan de xin shui 。 liang ge geng xin fen bie wan cheng qi lai hen jian dan , dan tong guo jiang zhe liang ge geng xin bao kuo zai beginTransaction() he commit() tiao yong zhong , jiu ke yi bao zheng zai geng gai wan cheng zhi qian , qi ta ren wu fa kan dao geng gai 。 ru guo fa sheng le cuo wu ,catch kuai ke yi hui gun shi wu kai shi yi lai fa sheng de suo you geng gai , bing da yin chu yi tiao cuo wu xiao xi 。 bing bu shi yi ding yao zai shi wu zhong zuo chu geng xin 。 nin ye ke yi fa chu fu duo de cha xun lai ti qu shu ju , huan ke yi shi yong na zhong xin xi gou jian geng duo de geng xin he cha xun 。 dang shi wu zai huo dong shi , ke yi bao zheng qi ta ren zai gong zuo jin xing dang zhong wu fa zuo chu geng gai 。 shi shi shang , zhe bu shi 100% de zheng que , dan ru guo nin zhi qian mei you ting shuo guo shi wu de hua , zhe yang jie shao ye wei chang bu ke 。 ★ yu chu li yu ju he cun chu guo cheng hen duo geng cheng shu de shu ju ku dou zhi chi yu chu li yu ju de gai nian 。 shen me shi yu chu li yu ju ? nin ke yi ba yu chu li yu ju kan zuo nin xiang yao yun xing de SQL de yi zhong bian yi guo de mo ban , ta ke yi shi yong bian liang shen shu jin xing ding zhi 。 yu chu li yu ju ke yi dai lai liang da hao chu : cha xun zhi xu jie xi ( huo zhun bei ) yi ci , dan shi ke yi yong xiang tong huo bu tong de shen shu zhi xing duo ci 。 dang cha xun zhun bei hao hou , shu ju ku jiang fen xi 、 bian yi he you hua zhi xing gai cha xun de ji hua 。 dui yu fu duo de cha xun , zhe ge guo cheng yao hua bi jiao chang de shi jian , ru guo nin xu yao yi bu tong shen shu duo ci chong fu xiang tong de cha xun , na me gai guo cheng jiang da da jiang di ying yong cheng xu de su du 。 tong guo shi yong yu chu li yu ju , ke yi bi mian chong fu fen xi / bian yi / you hua zhou qi 。 jian yan zhi , yu chu li yu ju shi yong geng shao de zi yuan , yin er yun xing de geng kuai 。 ti gong gei yu chu li yu ju de shen shu bu xu yao yong yin hao kuo qi lai , qu dong cheng xu hui chu li zhe xie 。 ru guo ying yong cheng xu du zhan di shi yong yu chu li yu ju , na me ke yi que bao mei you SQL ru qin fa sheng 。( ran er , ru guo nin reng ran jiang cha xun de qi ta bu fen jian li zai bu shou xin ren de shu ru zhi shang , na me jiu reng ran cun zai feng xian )。 yu chu li yu ju shi ru ci you yong , yi zhi PDO shi ji shang da po le zai mu biao 4 zhong she xia de gui ze : ru guo qu dong cheng xu bu zhi chi yu chu li yu ju , na me PDO jiang fang zhen yu chu li yu ju 。 shi li :PDO de ying yong li zi : <?php $dbms='mysql';// shu ju ku lei xing Oracle yong ODI, dui yu kai fa zhe lai shuo , shi yong bu tong de shu ju ku , zhi yao gai zhe ge , bu yong ji zhu na me duo de han shu le $host='localhost';// shu ju ku zhu ji ming $dbName='test';// shi yong de shu ju ku $user='root';// shu ju ku lian jie yong hu ming $pass='';// dui ying de mi ma $dsn="$dbms:host=$host;dbname=$dbName"; classdbextendsPDO{ publicfunction__construct(){ try{ parent::__construct("$GLOBALS[dsn]",$GLOBALS['user'],$GLOBALS['pass']); }catch(PDOException$e){ die("Error: ".$e->__toString()."<br/>"); } } publicfinalfunctionquery($sql){ try{ returnparent::query($this->setString($sql)); }catch(PDOException$e){ die("Error: ".$e->__toString()."<br/>"); } } privatefinalfunctionsetString($sql){ echo" wo yao chu li yi xia $sql"; return$sql; } } $db=newdb(); $db->setAttribute(PDO::ATTR_CASE,PDO::CASE_UPPER); foreach($db->query('SELECT * from xxxx_menu')as$row){ print_r($row); } $db->exec('DELETE FROM `xxxx_menu` where mid=43'); ?>
󰈣󰈤
  免责声明:本文仅代表作者个人观点,与王朝网络无关。王朝网络登载此文出于传递更多信息之目的,并不意味着赞同其观点或证实其描述,其原创性以及文中陈述文字和内容未经本站证实,对本文以及其中全部或者部分内容、文字的真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
 
 
时尚秀气模特裴裴(8)
时尚秀气模特裴裴(7)
时尚秀气模特裴裴(6)
时尚秀气模特裴裴(5)
白墙黑瓦
广州东郊[原创]
八美至丹巴的东谷天然盆景
不一样的
 
>>返回首页<<
 
 
 为你推荐
 
 
 
 转载本文
 UBB代码 HTML代码
复制到剪贴板...
 
 热帖排行
 
 
 
 
 
©2005- 王朝网络 版权所有