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&amp;&amp;mysql)六
Oct 09 PHP
SWFUpload与CI不能正确上传识别文件MIME类型解决方法分享
Apr 18 PHP
php和数据库结合的一个简单的web实例 代码分析 (php初学者)
Jul 28 PHP
php.ini修改php上传文件大小限制的方法详解
Jun 17 PHP
php页面缓存方法小结
Jan 10 PHP
php需登录的文件上传管理系统
Mar 21 PHP
总结PHP中DateTime的常用方法
Aug 11 PHP
Yii2中事务的使用实例代码详解
Sep 07 PHP
PHP中ajax无刷新上传图片与图片下载功能
Feb 21 PHP
TP3.2批量上传文件或图片 同名冲突问题的解决方法
Aug 01 PHP
Laravel 5.5 的自定义验证对象/类示例代码详解
Aug 29 PHP
tp5框架内使用tp3.2分页的方法分析
May 05 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封装的smarty类完整实例
2016/10/19 PHP
Laravel框架实现定时Task Scheduling例子
2019/10/22 PHP
详细分析PHP 命名空间(namespace)
2020/06/30 PHP
javascript 命名规则 变量命名规则
2010/02/25 Javascript
jQuery之日期选择器的深入解析
2013/06/19 Javascript
js实现浏览器的各种菜单命令比如打印、查看源文件等等
2013/10/24 Javascript
利用a标签自动解析URL分析网址实例
2014/10/20 Javascript
javascript动态创建链接的方法
2015/05/13 Javascript
深入理解Node.js 事件循环和回调函数
2016/11/02 Javascript
JavaScript的六种继承方式(推荐)
2017/06/26 Javascript
vue cli webpack中使用sass的方法
2018/02/24 Javascript
浅谈Vue2.0中v-for迭代语法的变化(key、index)
2018/03/06 Javascript
jsonp跨域及实现百度首页联想功能的方法
2018/08/30 Javascript
基于vue通用表单解决方案的思考与分析
2019/03/16 Javascript
6种JavaScript继承方式及优缺点(小结)
2020/02/06 Javascript
JS猜数字游戏实例讲解
2020/06/30 Javascript
[01:09]DOTA2次级职业联赛 - ishow.HMM战队宣传片
2014/12/01 DOTA
python中Genarator函数用法分析
2015/04/08 Python
Python使用ntplib库同步校准当地时间的方法
2016/07/02 Python
python遍历序列enumerate函数浅析
2017/10/17 Python
Python使用Scrapy爬虫框架全站爬取图片并保存本地的实现代码
2018/03/04 Python
python删除本地夹里重复文件的方法
2020/11/19 Python
Python3.6日志Logging模块简单用法示例
2018/06/14 Python
Python替换月份为英文缩写的实现方法
2019/07/15 Python
python实现梯度法 python最速下降法
2020/03/24 Python
HTML5之SVG 2D入门12—SVG DOM及DOM操作介绍
2013/01/30 HTML / CSS
非凡女性奢华谦虚风格:The Modist
2017/10/28 全球购物
TripAdvisor日本:全球领先的旅游网站
2019/02/14 全球购物
销售工作人员的自我评价分享
2013/11/10 职场文书
计算机专业毕业生自荐信
2013/12/31 职场文书
护理专业毕业生自荐信范文
2014/01/05 职场文书
2015大学生求职信范文
2015/03/20 职场文书
雷锋的故事观后感
2015/06/10 职场文书
退休教师追悼词
2015/06/23 职场文书
GO语言字符串处理函数之处理Strings包
2022/04/14 Golang
Windows7下FTP搭建图文教程
2022/08/05 Servers