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


Posted in PHP onMarch 31, 2018

本文实例讲述了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 相关文章推荐
用php实现批量查询清除一句话后门的代码
Jan 20 PHP
在php MYSQL中插入当前时间
Apr 06 PHP
PHP学习之数组的定义和填充
Apr 17 PHP
php中对2个数组相加的函数
Jun 24 PHP
用Json实现PHP与JavaScript间数据交换的方法详解
Jun 20 PHP
IIS安装Apache伪静态插件的具体操作图文
Jul 01 PHP
PHPCMS V9 添加二级导航的思路详解
Oct 20 PHP
详解PHP数据压缩、加解密(pack, unpack)
Dec 17 PHP
PHP中Notice错误常见解决方法
Apr 28 PHP
php json转换相关知识(小结)
Dec 21 PHP
php curl获取https页面内容,不直接输出返回结果的设置方法
Jan 15 PHP
YII2.0框架行为(Behavior)深入详解
Jul 26 PHP
ThinkPHP框架中使用Memcached缓存数据的方法
Mar 31 #PHP
PHPTree――php快速生成无限级分类
Mar 30 #PHP
CMSPRESS 10行代码搞定 PHP无限级分类2
Mar 30 #PHP
PHP实现动态删除XML数据的方法示例
Mar 30 #PHP
PHP实现动态添加XML中数据的方法
Mar 30 #PHP
PHP实现动态创建XML文档的方法
Mar 30 #PHP
php实现微信模板消息推送
Mar 30 #PHP
You might like
PHP中if和or运行效率对比
2014/12/12 PHP
AES加解密在php接口请求过程中的应用示例
2016/10/26 PHP
Laravel框架分页实现方法分析
2018/06/12 PHP
JQuery 解析多维的Json数据格式
2009/11/02 Javascript
了解jQuery技巧来提高你的代码
2010/01/08 Javascript
JS跨域总结
2012/08/30 Javascript
页面加载完成后再执行JS的jquery写法以及区别说明
2014/02/22 Javascript
ExtJS 4.2 Grid组件单元格合并的方法
2016/10/12 Javascript
Websocket协议详解及简单实例代码
2016/12/12 Javascript
详解angularjs结合pagination插件实现分页功能
2017/02/10 Javascript
详解使用angular-cli发布i18n多国语言Angular应用
2017/05/20 Javascript
详解angularJS+Ionic移动端图片上传的解决办法
2017/09/13 Javascript
vue和react等项目中更简单的实现展开收起更多等效果示例
2018/02/22 Javascript
vue :src 文件路径错误问题的解决方法
2018/05/15 Javascript
简述ES6新增关键字let与var的区别
2019/08/23 Javascript
JavaScript 自定义html元素鼠标右键菜单功能
2019/12/02 Javascript
Python使用ntplib库同步校准当地时间的方法
2016/07/02 Python
基于Python中capitalize()与title()的区别详解
2017/12/09 Python
详解Python with/as使用说明
2018/12/13 Python
Python ORM编程基础示例
2020/02/02 Python
django API 中接口的互相调用实例
2020/04/01 Python
python实现五子棋程序
2020/04/24 Python
Python读取图像并显示灰度图的实现
2020/12/01 Python
解决pycharm 格式报错tabs和space不一致问题
2021/02/26 Python
纯css3实现的鼠标悬停动画按钮
2014/12/23 HTML / CSS
KIKO MILANO西班牙官网:意大利领先的化妆品和护肤品品牌
2019/05/03 全球购物
经济实惠的名牌太阳镜和眼镜:Privé Revaux
2021/02/07 全球购物
小学门卫岗位职责
2013/12/17 职场文书
《晚上的太阳》教学反思
2014/04/23 职场文书
任命书怎么写
2014/06/04 职场文书
作风转变心得体会
2014/09/02 职场文书
教师师德师风自我剖析材料
2014/09/29 职场文书
2014年高数考试作弊检讨书
2014/12/14 职场文书
销售人员管理制度
2015/08/06 职场文书
如何使用CocosCreator对象池
2021/04/14 Javascript
Python Pandas pandas.read_sql函数实例用法
2021/06/21 Python