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 相关文章推荐
source.php查看源文件
Dec 09 PHP
php 文件状态缓存带来的问题
Dec 14 PHP
PHP 第二节 数据类型之数值型
Apr 28 PHP
浅析51个PHP处理字符串的函数
Aug 02 PHP
PHP实现把文本中的URL转换为链接的auolink()函数分享
Jul 29 PHP
Destoon实现多表查询示例
Aug 21 PHP
php猴子选大王问题解决方法
May 12 PHP
ThinkPHP函数详解之M方法和R方法
Sep 10 PHP
PHP中file_put_contents追加和换行的实现方法
Apr 01 PHP
ThinkPHP框架表单验证操作方法
Jul 19 PHP
php实现的PDO异常处理操作分析
Dec 27 PHP
PHP通过GD库实现验证码功能示例
Feb 23 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
DedeCMS dede_channeltype表字段注释
2010/04/07 PHP
php修改时间格式的代码
2011/05/29 PHP
php获取URL中带#号等特殊符号参数的解决方法
2014/09/02 PHP
PHP中让curl支持sock5的代码实例
2015/01/21 PHP
ThinkPHP路由机制简介
2016/03/23 PHP
PHP getID3类的使用方法学习笔记【附getID3源码下载】
2019/10/18 PHP
使用jquery与图片美化checkbox和radio控件的代码(打包下载)
2010/11/11 Javascript
js传中文参数controller里获取参数乱码问题解决方法
2014/01/03 Javascript
js控制再次点击按钮之间的间隔时间可防止重复提交
2014/08/01 Javascript
JS实现倒计时和文字滚动的效果实例
2014/10/29 Javascript
浅谈nodeName,nodeValue,nodeType,typeof 的区别
2015/01/13 Javascript
javascript三种代码注释方法
2016/06/02 Javascript
jQuery实现带遮罩层效果的blockUI弹出层示例【附demo源码下载】
2016/09/14 Javascript
在Vue.js中使用Mixins的方法
2017/09/12 Javascript
10行原生JS实现文字无缝滚动(超简单)
2018/01/02 Javascript
Bootstrap 实现表格样式、表单布局的实例代码
2018/12/09 Javascript
Vue 后台管理类项目兼容IE9+的方法示例
2019/02/20 Javascript
iview tabs 顶部导航栏和模块切换栏的示例代码
2019/03/04 Javascript
Vue3 实现双盒子定位Overlay的示例
2020/12/22 Vue.js
关于uniApp editor微信滑动问题
2021/01/15 Javascript
[53:03]Optic vs TNC 2018国际邀请赛小组赛BO2 第一场 8.17
2018/08/18 DOTA
[50:21]Liquid vs Winstrike 2018国际邀请赛小组赛BO2 第二场
2018/08/19 DOTA
Python实现保证只能运行一个脚本实例
2015/06/24 Python
Python+django实现文件下载
2016/01/17 Python
判断网页编码的方法python版
2016/08/12 Python
python爬虫实现教程转换成 PDF 电子书
2017/02/19 Python
Python提取支付宝和微信支付二维码的示例代码
2019/02/15 Python
python实现微信防撤回神器
2019/04/29 Python
用Q-learning算法实现自动走迷宫机器人的方法示例
2019/06/03 Python
Python 装饰器原理、定义与用法详解
2019/12/07 Python
OpenCV python sklearn随机超参数搜索的实现
2020/01/17 Python
Python GUI编程学习笔记之tkinter控件的介绍及基本使用方法详解
2020/03/30 Python
意向书范文
2014/03/31 职场文书
学校三节实施方案
2014/06/09 职场文书
2014党员民主评议个人总结
2014/09/10 职场文书
SpringBoot集成Redis,并自定义对象序列化操作
2021/06/22 Java/Android