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 相关文章推荐
?生?D片??C字串
Dec 06 PHP
php学习笔记 面向对象中[接口]与[多态性]的应用
Jun 16 PHP
PHP实现读取一个1G的文件大小
Aug 24 PHP
迅速确定php多维数组的深度的方法
Jan 07 PHP
php的sprintf函数的用法 控制浮点数格式
Feb 14 PHP
PHP.ini中配置屏蔽错误信息显示和保存错误日志的例子
May 12 PHP
ThinkPHP3.1新特性之多数据库操作更加完善
Jun 19 PHP
PHP保存带BOM文件的方法
Feb 12 PHP
php常用数组函数实例小结
Dec 29 PHP
PHP实现微信提现功能
Sep 30 PHP
Laravel 5.2 文档 数据库 ―― 起步介绍
Oct 21 PHP
laravel 执行迁移回滚示例
Oct 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
ThinkPHP的SAE开发相关注意事项详解
2016/10/09 PHP
Laravel 自带的Auth验证登录方法
2019/09/30 PHP
jQuery :nth-child前有无空格的区别分析
2011/07/11 Javascript
关于删除时的提示处理(确定删除吗)
2013/11/03 Javascript
JS实现遮罩层效果的简单实例
2013/11/12 Javascript
node.js使用nodemailer发送邮件实例
2014/03/10 Javascript
Javascript动态创建表格及删除行列的方法
2015/05/15 Javascript
javascript框架设计之种子模块
2015/06/23 Javascript
纯javascript实现图片延时加载方法
2015/08/21 Javascript
jQuery文本框得到与失去焦点动态改变样式效果
2016/09/08 Javascript
深入理解JavaScript中的块级作用域、私有变量与模块模式
2016/10/31 Javascript
浅析JavaScript中break、continue和return的区别
2016/11/30 Javascript
详解javascript获取url信息的常见方法
2016/12/19 Javascript
JS中判断字符串存在和非空的方法
2018/09/12 Javascript
vue设置动态请求地址的例子
2019/11/01 Javascript
js实现百度淘宝搜索功能
2020/02/17 Javascript
在vue中使用Base64转码的案例
2020/08/07 Javascript
bootstrap实现tab选项卡切换
2020/08/09 Javascript
解决Vue-cli无法编译es6的问题
2020/10/30 Javascript
[01:41]DOTA2超级联赛专访YYF 称一辈子难忘TI2
2013/05/28 DOTA
跟老齐学Python之字典,你还记得吗?
2014/09/20 Python
Python中asyncore的用法实例
2014/09/29 Python
浅谈python socket函数中,send与sendall的区别与使用方法
2017/05/09 Python
Python实现统计代码行的方法分析
2017/07/12 Python
python使用json序列化datetime类型实例解析
2018/02/11 Python
python中嵌套函数的实操步骤
2019/02/27 Python
Python3+Requests+Excel完整接口自动化测试框架的实现
2019/10/11 Python
HTML5+CSS3实例 :canvas 模拟实现电子彩票刮刮乐代码
2016/12/30 HTML / CSS
英国独特的时尚和生活方式品牌:JOY
2018/03/17 全球购物
Deichmann英国:德国鞋类零售商
2021/01/30 全球购物
物业管理专业个人的自我评价
2013/11/19 职场文书
优秀社区干部事迹材料
2014/02/03 职场文书
技校学生个人职业生涯规划范文
2014/03/03 职场文书
工程服务质量承诺书
2015/04/29 职场文书
MySQL GTID复制的具体使用
2022/05/20 MySQL
面试官问我Mysql的存储引擎了解多少
2022/08/05 MySQL