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 相关文章推荐
第二节 对象模型 [2]
Oct 09 PHP
php面向对象中的魔术方法中文说明
Mar 04 PHP
php中$美元符号与Zen Coding冲突问题解决方法分享
May 28 PHP
Codeigniter整合Tank Auth权限类库详解
Jun 12 PHP
YII实现分页的方法
Jul 09 PHP
PHP实现数组递归转义的方法
Aug 28 PHP
php绘图之加载外部图片的方法
Jan 24 PHP
PHP扩展开发入门教程
Feb 26 PHP
thinkPHP中volist标签用法示例
Dec 06 PHP
PHP单例模式定义与使用实例详解
Feb 06 PHP
php获取今日开始时间和结束时间的方法
Feb 27 PHP
php修改数组键名的方法示例
Apr 15 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
pdo中使用参数化查询sql
2011/08/11 PHP
PHP获取MSN好友列表类的实现代码
2013/06/23 PHP
php header功能的使用
2013/10/28 PHP
PHP中使用GD库绘制折线图 折线统计图的绘制方法
2015/11/09 PHP
Linux系统中为php添加pcntl扩展
2016/08/28 PHP
JavaScript类库D
2010/10/24 Javascript
5秒后跳转效果(setInterval/SetTimeOut)
2013/05/03 Javascript
js猜数字小游戏的简单实现代码
2013/07/02 Javascript
javascript操作css属性
2013/12/30 Javascript
javascript中的Function.prototye.bind
2015/06/25 Javascript
javascript跑马灯抽奖实例讲解
2020/04/17 Javascript
移动端js触摸事件详解
2016/09/18 Javascript
jQuery实现的分页功能示例
2017/01/22 Javascript
原生js开发的日历插件
2017/02/04 Javascript
Nodejs中使用phantom将html转为pdf或图片格式的方法
2017/09/18 NodeJs
JavaScript如何获取到导航条中HTTP信息
2017/10/10 Javascript
js 实现复选框只能选择一项的示例代码
2018/01/23 Javascript
JavaScript 跨域之POST实现方法
2018/05/07 Javascript
微信小程序6位或多位验证码密码输入框功能的实现代码
2018/05/29 Javascript
解决vuejs项目里css引用背景图片不能显示的问题
2018/09/13 Javascript
JavaScript Math对象和调试程序的方法分析
2019/05/13 Javascript
python实现linux服务器批量修改密码并生成execl
2014/04/22 Python
python爬虫爬取淘宝商品信息(selenum+phontomjs)
2018/02/24 Python
python多维数组切片方法
2018/04/13 Python
python求质数的3种方法
2018/09/28 Python
python调用c++ ctype list传数组或者返回数组的方法
2019/02/13 Python
python 利用pyttsx3文字转语音过程详解
2019/09/25 Python
Python数据分析库pandas高级接口dt的使用详解
2020/12/11 Python
HTML5实现页面切换激活的PageVisibility API使用初探
2016/05/13 HTML / CSS
加拿大在线隐形眼镜专家:PerfectLens.ca
2016/11/19 全球购物
高中毕业自我鉴定
2013/12/16 职场文书
电子商务自荐书范文
2014/01/04 职场文书
小学教师个人先进事迹材料
2014/05/17 职场文书
恋恋笔记本观后感
2015/06/16 职场文书
Python中itertools库的四个函数介绍
2022/04/06 Python
分享node.js实现简单登录注册的具体代码
2022/04/26 NodeJs