今天就跟大家聊聊有关如何在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;
一、phpStudy,是一个新手入门最常用的开发环境。二、WampServer,WampServer也同样的也是和phpStudy一样操作简单对小白比较友好。三、XAMPP,XAMPP(Apache+MySQL+PHP+PERL)是一个功能强大的建站集成软件包;四、MAMP,MAMP分为两种MAMP和MAMP Pro for Mac。五、宝塔面板,宝塔面板是一款服务器管理软件,支持windows和linux系统。六、UPUPW,UPUPW是目前Windows平台下最具特色的Web服务器PHP套件。
看完上述内容,你们对如何在PHP中使用PDO扩展连接PostgreSQL数据库有进一步的了解吗?如果还想了解更多知识或者相关内容,请关注亿速云行业资讯频道,感谢大家的支持。
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。