• 欢迎访问搞代码网站,推荐使用最新版火狐浏览器和Chrome浏览器访问本网站!
  • 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏搞代码吧

PDO扩展连接PostgreSQL对象关系数据库步骤详解

php 搞代码 4年前 (2022-01-22) 16次浏览 已收录 0个评论

这次给大家带来PDO扩展连接PostgreSQL对象关系数据库步骤详解,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);    $myPDOS<p style="color:transparent">2本文来源gao!daima.com搞$代!码网</p><span>搞代gaodaima码</span>tatement->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;

搞代码网(gaodaima.com)提供的所有资源部分来自互联网,如果有侵犯您的版权或其他权益,请说明详细缘由并提供版权或权益证明然后发送到邮箱[email protected],我们会在看到邮件的第一时间内为您处理,或直接联系QQ:872152909。本网站采用BY-NC-SA协议进行授权
转载请注明原文链接:PDO扩展连接PostgreSQL对象关系数据库步骤详解
喜欢 (0)
[搞代码]
分享 (0)
发表我的评论
取消评论

表情 贴图 加粗 删除线 居中 斜体 签到

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址