国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 語言 > PHP > 正文

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

2024-05-05 00:02:59
字體:
來源:轉載
供稿:網友

本文實例講述了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教程頻道。
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表

圖片精選

主站蜘蛛池模板: 和林格尔县| 长垣县| 启东市| 军事| 菏泽市| 罗源县| 汝城县| 达日县| 雷州市| 房产| 大洼县| 饶平县| 利辛县| 大余县| 大洼县| 晋江市| 罗江县| 启东市| 筠连县| 永新县| 泽库县| 东丰县| 夏邑县| 徐水县| 崇明县| 胶南市| 凤山县| 民乐县| 阿合奇县| 乌鲁木齐市| 开平市| 安泽县| 渭南市| 昌平区| 定兴县| 淮滨县| 黑水县| 鄂伦春自治旗| 衡东县| 沽源县| 临漳县|