PHP实现基于PDO扩展连接PostgreSQL对象关系数据库示例

发布时间:2022-04-30 发布网站:脚本宝典
脚本宝典收集整理的这篇文章主要介绍了PHP实现基于PDO扩展连接PostgreSQL对象关系数据库示例脚本宝典觉得挺不错的,现在分享给大家,也给大家做个参考。

本文实例讲述了PHP实现基于PDO扩展连接Postgresql对象关系数据库方法分享给大家供大家参考,具体如下:

<PRe class="brush:PHp;"> $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',"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实现基于PDO扩展连接PostgreSQL对象关系数据库示例全部内容,希望文章能够帮你解决PHP实现基于PDO扩展连接PostgreSQL对象关系数据库示例所遇到的问题。

如果觉得脚本宝典网站内容还不错,欢迎将脚本宝典推荐好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。