主頁(yè) > 知識(shí)庫(kù) > PHP實(shí)現(xiàn)基于PDO擴(kuò)展連接PostgreSQL對(duì)象關(guān)系數(shù)據(jù)庫(kù)示例

PHP實(shí)現(xiàn)基于PDO擴(kuò)展連接PostgreSQL對(duì)象關(guān)系數(shù)據(jù)庫(kù)示例

熱門標(biāo)簽:科大訊飛語(yǔ)音識(shí)別系統(tǒng) 服務(wù)器配置 銀行業(yè)務(wù) Mysql連接數(shù)設(shè)置 Linux服務(wù)器 阿里云 團(tuán)購(gòu)網(wǎng)站 電子圍欄

本文實(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常見(jiàn)數(shù)據(jù)庫(kù)操作技巧匯總》

希望本文所述對(duì)大家PHP程序設(shè)計(jì)有所幫助。

您可能感興趣的文章:
  • python連接PostgreSQL數(shù)據(jù)庫(kù)的過(guò)程詳解
  • docker環(huán)境下數(shù)據(jù)庫(kù)的備份(postgresql, mysql) 實(shí)例代碼
  • C# 操作PostgreSQL 數(shù)據(jù)庫(kù)的示例代碼
  • 在Ubuntu中安裝Postgresql數(shù)據(jù)庫(kù)的步驟詳解
  • PostgreSQL數(shù)據(jù)庫(kù)中跨庫(kù)訪問(wèn)解決方案
  • Python連接PostgreSQL數(shù)據(jù)庫(kù)的方法
  • PostgreSQL將數(shù)據(jù)加載到buffer cache中操作方法

標(biāo)簽:江蘇 蚌埠 衢州 棗莊 廣元 衡水 萍鄉(xiāng) 大理

巨人網(wǎng)絡(luò)通訊聲明:本文標(biāo)題《PHP實(shí)現(xiàn)基于PDO擴(kuò)展連接PostgreSQL對(duì)象關(guān)系數(shù)據(jù)庫(kù)示例》,本文關(guān)鍵詞  ;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問(wèn)題,煩請(qǐng)?zhí)峁┫嚓P(guān)信息告之我們,我們將及時(shí)溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無(wú)關(guān)。
  • 相關(guān)文章
  • 收縮
    • 微信客服
    • 微信二維碼
    • 電話咨詢

    • 400-1100-266