婷婷综合国产,91蜜桃婷婷狠狠久久综合9色 ,九九九九九精品,国产综合av

主頁 > 知識庫 > PHP實現基于PDO擴展連接PostgreSQL對象關系數據庫示例

PHP實現基于PDO擴展連接PostgreSQL對象關系數據庫示例

熱門標簽:上海做外呼線路的通信公司 福建銀行智能外呼系統價格 電話機器人銷售主要負責什么 地圖標注專員怎么樣 四川保險智能外呼系統供應商 遼寧ai電銷機器人價格 寧波外呼營銷系統 長沙做地圖標注公司 房產中介用的是什么外呼系統

本文實例講述了PHP實現基于PDO擴展連接PostgreSQL對象關系數據庫的方法。分享給大家供大家參考,具體如下:

$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;

更多關于PHP相關內容感興趣的讀者可查看本站專題:《PHP基于pdo操作數據庫技巧總結》、《php+Oracle數據庫程序設計技巧總結》、《PHP+MongoDB數據庫操作技巧大全》、《php面向對象程序設計入門教程》、《php字符串(string)用法總結》、《php+mysql數據庫操作入門教程》及《php常見數據庫操作技巧匯總》

希望本文所述對大家PHP程序設計有所幫助。

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

標簽:宿遷 工商登記 深圳 宜春 常德 佛山 延安 澳門

巨人網絡通訊聲明:本文標題《PHP實現基于PDO擴展連接PostgreSQL對象關系數據庫示例》,本文關鍵詞  PHP,實現,基于,PDO,擴展,連接,;如發現本文內容存在版權問題,煩請提供相關信息告之我們,我們將及時溝通與處理。本站內容系統采集于網絡,涉及言論、版權與本站無關。
  • 相關文章
  • 下面列出與本文章《PHP實現基于PDO擴展連接PostgreSQL對象關系數據庫示例》相關的同類信息!
  • 本頁收集關于PHP實現基于PDO擴展連接PostgreSQL對象關系數據庫示例的相關信息資訊供網民參考!
  • 推薦文章
    主站蜘蛛池模板: 柞水县| 霸州市| 麻江县| 南溪县| 稷山县| 青浦区| 蒙城县| 平度市| 东光县| 宁明县| 颍上县| 金秀| 百色市| 焉耆| 麻江县| 庐江县| 临泽县| 日土县| 慈溪市| 始兴县| 金川县| 正蓝旗| 英德市| 宣恩县| 巴塘县| 石河子市| 化德县| 保亭| 黄龙县| 昌宁县| 建湖县| 聂拉木县| 荃湾区| 永靖县| 天峻县| 琼中| 织金县| 衢州市| 连山| 东山县| 盖州市|