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

 2025-01-15  阅读 279  评论 8  点赞 458

摘要:本文实例讲述了php实现基于pdo扩展连接postgresql对象关系数据库的方法。分享给大家供大家参考,具体如下: $pdo = null; if(version_compare(php_version, '5.3.6', '

本文实例讲述了php实现基于pdo扩展连接postgresql对象关系数据库的方法。分享给大家供大家参考,具体如下:

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程序设计有所帮助。


标签:phpphp教程

评论列表:

  •   weihang666
     发布于 3天前回复该评论
  • 又学到了新知识!
  •   xixi2
     发布于 2天前回复该评论
  • 写的很不错,学到了!
  •   xixi2
     发布于 2天前回复该评论
  • 写的很不错,学到了!
显示更多评论

发表评论:

管理员

承接各种程序开发,外贸网站代运营,外贸网站建设等项目
  • 内容2460
  • 积分67666
  • 金币86666

Copyright © 2024 LS'Blog-保定PHP程序员老宋个人博客 Inc. 保留所有权利。 Powered by LS'blog 3.0.3

页面耗时0.0280秒, 内存占用1.95 MB, 访问数据库30次

冀ICP备19034377号