欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

PHP实现基于PDO扩展连接PostgreSQL对象关系数据库示例

程序员文章站 2022-08-13 16:07:29
本文实例讲述了php实现基于pdo扩展连接postgresql对象关系数据库的方法。分享给大家供大家参考,具体如下: $pdo = null; if(versi...

本文实例讲述了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程序设计有所帮助。