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编程之高级技巧——利用Mysql函数
Oct 09 PHP
PHP 采集获取指定网址的内容
Jan 05 PHP
php中通过curl模拟登陆discuz论坛的实现代码
Feb 16 PHP
PHP常用特殊运算符号和函数总结(php新手入门必看)
Feb 02 PHP
CI框架中通过hook的方式实现简单的权限控制
Jan 07 PHP
PHP多维数组元素操作类的方法
Nov 14 PHP
php PDO异常处理详解
Nov 20 PHP
详解PHP中的序列化、反序列化操作
Mar 21 PHP
PHP 7安装调试工具Xdebug扩展的方法教程
Jun 17 PHP
PHP实现求解最长公共子串问题的方法
Nov 17 PHP
PHP微信H5支付开发实例
Jul 25 PHP
PHP精确到毫秒秒杀倒计时实例详解
Mar 14 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实现查询百度google收录情况(示例代码)
2013/08/02 PHP
PHP环境中Memcache的安装和使用
2015/11/05 PHP
使用JavaScript创建新样式表和新样式规则
2016/06/14 PHP
PHP中in_array的隐式转换的解决方法
2018/03/06 PHP
PHP以json或xml格式返回请求数据的方法
2018/05/31 PHP
ThinkPHP框架整合微信支付之JSAPI模式图文详解
2019/04/09 PHP
Yii2框架中一些折磨人的坑
2019/12/15 PHP
JS性能优化笔记搜索整理
2013/08/21 Javascript
IE8下String的Trim()方法失效的解决方法
2013/11/08 Javascript
JS判断浏览器是否支持某一个CSS3属性的方法
2014/10/17 Javascript
javascript实现ecshop搜索框键盘上下键切换控制
2015/03/18 Javascript
jQuery弹出层插件Lightbox_me使用指南
2015/04/21 Javascript
Jquery中的$.each获取各种返回类型数据的使用方法
2015/05/03 Javascript
js获取页面引用的css样式表中的属性值方法(推荐)
2016/08/19 Javascript
jQuery与js实现颜色渐变的方法
2016/12/30 Javascript
js输入框使用正则表达式校验输入内容的实例
2017/02/12 Javascript
JavaScript实现微信号随机切换代码
2018/03/09 Javascript
基于js判断浏览器是否支持webGL
2020/04/18 Javascript
解决vant的Toast组件时提示not defined的问题
2020/11/11 Javascript
Vue中引入svg图标的两种方式
2021/01/14 Vue.js
[02:49]DOTA2完美大师赛首日观众采访
2017/11/23 DOTA
python matplotlib 注释文本箭头简单代码示例
2018/01/08 Python
Python tkinter事件高级用法实例
2018/01/31 Python
Numpy之文件存取的示例代码
2018/08/03 Python
python每天定时运行某程序代码
2019/08/16 Python
css3.0 图形构成实例练习一
2013/03/19 HTML / CSS
倩碧美国官网:Clinique美国
2016/07/20 全球购物
美国女性奢华品牌精品店:INTERMIX
2017/10/12 全球购物
求网格中的黑点分布
2013/11/06 面试题
本科生个人求职自荐信
2013/09/26 职场文书
感恩节活动方案
2014/01/27 职场文书
求职信标题怎么写
2014/05/26 职场文书
学校教研活动总结
2014/07/02 职场文书
2015年度酒店客房部工作总结
2015/05/25 职场文书
基督教追悼会答谢词
2015/09/29 职场文书
MySQL CHAR和VARCHAR该如何选择
2021/05/31 MySQL