PHP实现基于PDO扩展连接PostgreSQL对象关系数据库示例
发布:smiling 来源: PHP粉丝网 添加日期:2021-09-06 10:12:46 浏览: 评论:0
这篇文章主要介绍了PHP实现基于PDO扩展连接PostgreSQL对象关系数据库,结合实例形式分析了php使用pdo连接PostgreSQL并执行简单sql语句的相关操作技巧,需要的朋友可以参考下。
本文实例讲述了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;
Tags: PDO PostgreSQL
相关文章
- ·php pdo_mysql未安装问题解决方法(2013-12-07)
- ·pdo用法学习笔记(2013-12-27)
- ·PHP用PDO防Sql注入注意事项(2014-08-21)
- ·PHP防sql注入方法总结分析(2014-08-23)
- ·Pdo_MySQL安装过程模块提示PDO_MYSQL make: *** [pdo_mysql.lo](2014-08-28)
- ·Php中使用PDO操作MySQL数据库(查询 更新 删除)(2014-09-10)
- ·php PDO 使用方法详解(2014-09-10)
- ·php下pdo的mysql事务处理应用实例(2014-09-10)
- ·php pdo连接mssql server数据库(2014-09-11)
- ·PDO 中相关的错误处理(2014-09-11)
- ·pdo操作数据库入门教程(2014-09-11)
- ·php pdo连接并查询sql数据库代码(2014-09-11)
- ·php中PDO入门学习笔记(2015-04-08)
- ·深入分析PHP PDO配置及使用方法(2015-04-08)
- ·PHP单例模式编写的PDO类的程序(2015-04-15)
- ·PHP使用PDO抽象层获取查询结果的方法示例(2018-11-07)
推荐文章
热门文章
最新评论文章
- 写给考虑创业的年轻程序员(10)
- PHP新手上路(一)(7)
- 惹恼程序员的十件事(5)
- PHP邮件发送例子,已测试成功(5)
- 致初学者:PHP比ASP优秀的七个理由(4)
- PHP会被淘汰吗?(4)
- PHP新手上路(四)(4)
- 如何去学习PHP?(2)
- 简单入门级php分页代码(2)
- php中邮箱email 电话等格式的验证(2)