php使用PDO扩展连接PostgreSQL对象关系数据库
来源:互联网 发布:sql将string转int 编辑:程序博客网 时间:2024/05/17 01:00
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;
0 0
- php使用PDO扩展连接PostgreSQL对象关系数据库
- PHP数据库对象-PDO拓展 使用pdo连接数据库
- PHP使用pdo连接数据库
- PHP PDO 数据库的连接与使用
- php使用PDO连接mysql数据库
- php使用PDO连接操作数据库
- PHP--使用PDO连接Mysql数据库
- php pdo连接数据库
- php PDO面向对象的方式连接数据库
- php pdo 连接db2 数据库
- php pdo 连接MySQL数据库
- php pdo连接mysql数据库
- xampp下PHP使用PDO 支持postgresql
- PHP使用PDO连接Access数据库的方法
- PHP使用PDO连接数据库could not find driver解决方法
- php pdo连接mysql必须扩展
- PHP使用PDO连接SQLITE3
- centos下安装php的PDO PostgreSQL扩展
- git使用
- linux命令:vim
- Jquery弹出Alert,Confirm,Prompt对话窗
- 网络营销重在增加目标流量
- 前端开发问题(JS部分)
- php使用PDO扩展连接PostgreSQL对象关系数据库
- android jni的一些总结
- docker学习笔记
- 【LEETCODE】105-Construct Binary Tree from Preorder and Inorder Traversal
- fragment replace 周期
- Rails--自动清除开发日志
- java基础学习笔记(8)
- 在JAVA代码中控制文本的显示行数
- A+B in Hogwarts