PHP實(shí)現(xiàn)基于PDO擴(kuò)展連接PostgreSQL對(duì)象關(guān)系數(shù)據(jù)庫(kù)示例
本文實(shí)例講述了PHP實(shí)現(xiàn)基于PDO擴(kuò)展連接PostgreSQL對(duì)象關(guān)系數(shù)據(jù)庫(kù)的方法。分享給大家供大家參考,具體如下:
$pdo = NULL; if(version_compare(PHP_VERSION, '5.3.6', '<')){ $pdo = new \PDO('pgsql:host=127.0.0.1;port=5432;dbname=postgredb1','postgres',"123456",array(\PDO::MYSQL_ATTR_INIT_COMMAND=>'SET NAMES \'UTF8\'' )); } else{ $pdo = new \PDO('pgsql:host=127.0.0.1;port=5432;dbname=postgredb1','postgres',"123456"); } try { $pdo->beginTransaction(); $tableName = 'user'; if($fetch = true){ $myPDOStatement = $pdo->prepare("SELECT * FROM " . $tableName . " WHERE id=:id "); if(!$myPDOStatement) { $errorInfo = $myPDOStatement->errorInfo(); throw new \Exception($errorInfo[0].'###'.$errorInfo[1].'###'.$errorInfo[2]); } $id = 1; $myPDOStatement->bindParam(":id",$id); $myPDOStatement->execute(); if($myPDOStatement->errorCode()>0){ $errorInfo = $myPDOStatement->errorInfo(); throw new \Exception($errorInfo[0].'###'.$errorInfo[1].'###'.$errorInfo[2]); } $item = $myPDOStatement->fetch(); print_r($item); } $insertedId = 0; if($insert = true){ $myPDOStatement = $pdo->prepare("INSERT INTO " . $tableName . "(username,password,status) VALUES(:username,:password,:status)"); if(!$myPDOStatement) { $errorInfo = $myPDOStatement->errorInfo(); throw new \Exception($errorInfo[0].'###'.$errorInfo[1].'###'.$errorInfo[2]); } $timestamp = time(); $data = array( 'username' =>'usernamex', 'password' =>'passwordx', 'status' =>'1', ); $myPDOStatement->bindParam(":username",$data['username']); $myPDOStatement->bindParam(":password",$data['password']); $myPDOStatement->bindParam(":status",$data['status']); $myPDOStatement->execute(); if($myPDOStatement->errorCode()>0){ $errorInfo = $myPDOStatement->errorInfo(); throw new \Exception($errorInfo[0].'###'.$errorInfo[1].'###'.$errorInfo[2]); } $affectRowCount = $myPDOStatement->rowCount(); if($affectRowCount>0){ $insertedId = $pdo->lastInsertId(); } print_r('$insertedId = '.$insertedId);//PostgreSQL不支持 print_r('$affectRowCount = '.$affectRowCount); } if($update = true){ $myPDOStatement = $pdo->prepare("UPDATE " . $tableName . " SET username=:username, status=:status WHERE id=:id"); if(!$myPDOStatement) { $errorInfo = $myPDOStatement->errorInfo(); throw new \Exception($errorInfo[0].'###'.$errorInfo[1].'###'.$errorInfo[2]); } $id = 1; $username = 'username update'; $status = 0; $myPDOStatement->bindParam(":id",$id); $myPDOStatement->bindParam(":username",$username); $myPDOStatement->bindParam(":status",$status); $myPDOStatement->execute(); if($myPDOStatement->errorCode()>0){ $errorInfo = $myPDOStatement->errorInfo(); throw new \Exception($errorInfo[0].'###'.$errorInfo[1].'###'.$errorInfo[2]); } $affectRowCount = $myPDOStatement->rowCount(); print_r('$affectRowCount = '.$affectRowCount); } if($fetchAll = true){ $myPDOStatement = $pdo->prepare("SELECT * FROM " . $tableName ." WHERE id > :id"); if(!$myPDOStatement) { $errorInfo = $myPDOStatement->errorInfo(); throw new \Exception($errorInfo[0].'###'.$errorInfo[1].'###'.$errorInfo[2]); } $id = 0; $myPDOStatement->bindParam(":id",$id); $myPDOStatement->execute(); if($myPDOStatement->errorCode()>0){ $errorInfo = $myPDOStatement->errorInfo(); throw new \Exception($errorInfo[0].'###'.$errorInfo[1].'###'.$errorInfo[2]); } $list = $myPDOStatement->fetchAll(); print_r($list); } if($update = true){ $myPDOStatement = $pdo->prepare("DELETE FROM " . $tableName . " WHERE id=:id"); if(!$myPDOStatement) { $errorInfo = $myPDOStatement->errorInfo(); throw new \Exception($errorInfo[0].'###'.$errorInfo[1].'###'.$errorInfo[2]); } //$insertedId = 10; $myPDOStatement->bindParam(":id",$insertedId); $myPDOStatement->execute(); if($myPDOStatement->errorCode()>0){ $errorInfo = $myPDOStatement->errorInfo(); throw new \Exception($errorInfo[0].'###'.$errorInfo[1].'###'.$errorInfo[2]); } $affectRowCount = $myPDOStatement->rowCount(); print_r('$affectRowCount = '.$affectRowCount); } $pdo->commit(); } catch (\Exception $e) { $pdo->rollBack(); // print_r($e); } $pdo = null;
更多關(guān)于PHP相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《PHP基于pdo操作數(shù)據(jù)庫(kù)技巧總結(jié)》、《php+Oracle數(shù)據(jù)庫(kù)程序設(shè)計(jì)技巧總結(jié)》、《PHP+MongoDB數(shù)據(jù)庫(kù)操作技巧大全》、《php面向?qū)ο蟪绦蛟O(shè)計(jì)入門教程》、《php字符串(string)用法總結(jié)》、《php+mysql數(shù)據(jù)庫(kù)操作入門教程》及《php常見數(shù)據(jù)庫(kù)操作技巧匯總》
希望本文所述對(duì)大家PHP程序設(shè)計(jì)有所幫助。
相關(guān)文章
理解PHP5中static和const關(guān)鍵字的區(qū)別
理解PHP5中static和const關(guān)鍵字的區(qū)別...2007-03-03php獲取遠(yuǎn)程https內(nèi)容時(shí)提示PHP Warning: copy(): Unable&
這篇文章主要介紹了php獲取遠(yuǎn)程https內(nèi)容時(shí)提示PHP Warning: copy(): Unable to find the wrapper “https“ 解決方法,文中通過代碼和圖文給大家講解的了解決方案,具有一定的參考價(jià)值,需要的朋友可以參考下2024-10-10JS(jQuery)實(shí)現(xiàn)聊天接收到消息語(yǔ)言自動(dòng)提醒功能詳解【提示“您有新的消息請(qǐng)注意查收”】
這篇文章主要介紹了JS(jQuery)實(shí)現(xiàn)聊天接收到消息語(yǔ)言自動(dòng)提醒功能,結(jié)合實(shí)例形式詳細(xì)分析了javascript結(jié)合ajax后臺(tái)交互實(shí)現(xiàn)信息語(yǔ)音提示功能相關(guān)原理與操作技巧,需要的朋友可以參考下2019-04-04PHP之生成GIF動(dòng)畫的實(shí)現(xiàn)方法
本篇文章是對(duì)PHP生成GIF動(dòng)畫的方法進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下2013-06-06實(shí)測(cè)在class的function中include的文件中非php的global全局環(huán)境
正如標(biāo)題所言經(jīng)測(cè)試的結(jié)果為:在class中include后,被include文件變量域已經(jīng)變成func中了,非全局.但是可以通過global提升,感興趣的朋友可以參考下哈,希望對(duì)大家有所幫助2013-07-07PHP 常用數(shù)組內(nèi)部函數(shù)(Array Functions)介紹
本章節(jié)我們還要學(xué)習(xí)一些其它常用的有關(guān)數(shù)組的內(nèi)部函數(shù):count,sizeof、sort、asort、ksort等等,感興趣的朋友可以參考下哈,希望對(duì)大家有所幫助2013-06-06