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执行速度全攻略
Oct 09 PHP
使用sockets:从新闻组中获取文章(三)
Oct 09 PHP
smarty section简介与用法分析
Oct 03 PHP
php遍历文件夹和文件列表示例分享
Mar 11 PHP
PHP利用APC模块实现文件上传进度条的方法
Jan 26 PHP
php计算2个日期的差值函数分享
Feb 02 PHP
php通过exif_read_data函数获取图片的exif信息
May 21 PHP
分享ThinkPHP3.2中关联查询解决思路
Sep 20 PHP
PHP中Closure类的使用方法及详解
Oct 09 PHP
PHP面向对象程序设计实例分析
Jan 26 PHP
PHP中如何防止外部恶意提交调用ajax接口
Apr 11 PHP
浅谈PHP eval()函数定义和用法
Jun 21 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
discuz程序的PHP加密函数原理分析
2011/08/05 PHP
PHP rsa加密解密使用方法
2015/04/27 PHP
PHP如何将log信息写入服务器中的log文件
2015/07/29 PHP
PHP 极验验证码实例讲解
2016/09/29 PHP
php array_keys 返回数组的键名
2016/10/25 PHP
完美的php分页类
2017/10/24 PHP
javascript 有趣而诡异的数组
2009/04/06 Javascript
javascript 支持链式调用的异步调用框架Async.Operation
2009/08/04 Javascript
jQuery学习总结之元素的相对定位和选择器(持续更新)
2011/04/26 Javascript
js 字符串转化成数字的代码
2011/06/29 Javascript
改变状态栏文字的js代码
2014/06/13 Javascript
基于jQuery实现动态数字展示效果
2015/08/12 Javascript
jQuery ready()和onload的加载耗时分析
2016/09/08 Javascript
jQuery检查元素存在性(推荐)
2016/09/17 Javascript
利用adb shell和node.js实现抖音自动抢红包功能(推荐)
2018/02/22 Javascript
详解Vue微信授权登录前后端分离较为优雅的解决方案
2018/06/29 Javascript
js canvas实现红包照片效果
2018/08/21 Javascript
koa2 数据api中间件设计模型的实现方法
2020/07/13 Javascript
Android分包MultiDex策略详解
2017/10/30 Python
python中学习K-Means和图片压缩
2017/11/20 Python
Python3 Random模块代码详解
2017/12/04 Python
python 解压pkl文件的方法
2018/10/25 Python
python远程调用rpc模块xmlrpclib的方法
2019/01/11 Python
简单的Python调度器Schedule详解
2019/08/30 Python
Python中的X[:,0]、X[:,1]、X[:,:,0]、X[:,:,1]、X[:,m:n]和X[:,:,m:n]
2020/02/13 Python
基于Keras中Conv1D和Conv2D的区别说明
2020/06/19 Python
Gap工厂店:Gap Factory
2017/11/02 全球购物
Geekbuying波兰:购买中国电子产品
2019/10/20 全球购物
课堂教学改革实施方案
2014/03/17 职场文书
企业负责人任命书
2014/06/05 职场文书
计划生育工作总结2015
2015/04/03 职场文书
生日赠语
2015/06/23 职场文书
Java基于字符界面的简易收银台
2021/06/26 Java/Android
html5+实现plus.io进行拍照和图片等获取
2022/06/01 HTML / CSS
python语言中pandas字符串分割str.split()函数
2022/08/05 Python
Debian11 Xfce终端光标的颜色怎么设置?
2022/08/14 数码科技