当前位置:首页 > PHP教程 > php高级应用 > 列表

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

发布:smiling 来源: PHP粉丝网  添加日期:2021-09-06 10:12:46 浏览: 评论:0 

这篇文章主要介绍了PHP实现基于PDO扩展连接PostgreSQL对象关系数据库,结合实例形式分析了php使用pdo连接PostgreSQL并执行简单sql语句的相关操作技巧,需要的朋友可以参考下。

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

  1. $pdo = NULL; 
  2. if(version_compare(PHP_VERSION, '5.3.6', '<')){ 
  3.   $pdo = new \PDO('pgsql:host=127.0.0.1;port=5432;dbname=postgredb1','postgres',"123456",array(\PDO::MYSQL_ATTR_INIT_COMMAND=>'SET NAMES \'UTF8\'' )); 
  4. } 
  5. else{ 
  6.   $pdo = new \PDO('pgsql:host=127.0.0.1;port=5432;dbname=postgredb1','postgres',"123456"); 
  7. } 
  8. try { 
  9.   $pdo->beginTransaction(); 
  10.   $tableName = 'user'; 
  11.   if($fetch = true){ 
  12.     $myPDOStatement = $pdo->prepare("SELECT * FROM " . $tableName . " WHERE id=:id "); 
  13.     if(!$myPDOStatement) { 
  14.       $errorInfo = $myPDOStatement->errorInfo(); 
  15.       throw new \Exception($errorInfo[0].'###'.$errorInfo[1].'###'.$errorInfo[2]); 
  16.     } 
  17.     $id = 1; 
  18.     $myPDOStatement->bindParam(":id",$id); 
  19.     $myPDOStatement->execute(); 
  20.     if($myPDOStatement->errorCode()>0){ 
  21.       $errorInfo = $myPDOStatement->errorInfo(); 
  22.       throw new \Exception($errorInfo[0].'###'.$errorInfo[1].'###'.$errorInfo[2]); 
  23.     } 
  24.     $item = $myPDOStatement->fetch(); 
  25.     print_r($item); 
  26.   } 
  27.   $insertedId = 0; 
  28.   if($insert = true){ 
  29.     $myPDOStatement = $pdo->prepare("INSERT INTO " . $tableName . "(username,password,status)  VALUES(:username,:password,:status)"); 
  30.     if(!$myPDOStatement) { 
  31.       $errorInfo = $myPDOStatement->errorInfo(); 
  32.       throw new \Exception($errorInfo[0].'###'.$errorInfo[1].'###'.$errorInfo[2]); 
  33.     } 
  34.     $timestamp = time(); 
  35.     $data = array( 
  36.       'username' =>'usernamex', 
  37.       'password' =>'passwordx', 
  38.       'status' =>'1', 
  39.     ); 
  40.     $myPDOStatement->bindParam(":username",$data['username']); 
  41.     $myPDOStatement->bindParam(":password",$data['password']); 
  42.     $myPDOStatement->bindParam(":status",$data['status']); 
  43.     $myPDOStatement->execute(); 
  44.     if($myPDOStatement->errorCode()>0){ 
  45.       $errorInfo = $myPDOStatement->errorInfo(); 
  46.       throw new \Exception($errorInfo[0].'###'.$errorInfo[1].'###'.$errorInfo[2]); 
  47.     } 
  48.     $affectRowCount = $myPDOStatement->rowCount(); 
  49.     if($affectRowCount>0){ 
  50.       $insertedId = $pdo->lastInsertId(); 
  51.     } 
  52.     print_r('$insertedId = '.$insertedId);//PostgreSQL不支持 
  53.     print_r('$affectRowCount = '.$affectRowCount); 
  54.   } 
  55.   if($update = true){ 
  56.     $myPDOStatement = $pdo->prepare("UPDATE " . $tableName . " SET username=:username, status=:status WHERE id=:id"); 
  57.     if(!$myPDOStatement) { 
  58.       $errorInfo = $myPDOStatement->errorInfo(); 
  59.       throw new \Exception($errorInfo[0].'###'.$errorInfo[1].'###'.$errorInfo[2]); 
  60.     } 
  61.     $id = 1; 
  62.     $username = 'username update'; 
  63.     $status = 0; 
  64.     $myPDOStatement->bindParam(":id",$id); 
  65.     $myPDOStatement->bindParam(":username",$username); 
  66.     $myPDOStatement->bindParam(":status",$status); 
  67.     $myPDOStatement->execute(); 
  68.     if($myPDOStatement->errorCode()>0){ 
  69.       $errorInfo = $myPDOStatement->errorInfo(); 
  70.       throw new \Exception($errorInfo[0].'###'.$errorInfo[1].'###'.$errorInfo[2]); 
  71.     } 
  72.     $affectRowCount = $myPDOStatement->rowCount(); 
  73.     print_r('$affectRowCount = '.$affectRowCount); 
  74.   } 
  75.   if($fetchAll = true){ 
  76.     $myPDOStatement = $pdo->prepare("SELECT * FROM " . $tableName ." WHERE id > :id"); 
  77.     if(!$myPDOStatement) { 
  78.       $errorInfo = $myPDOStatement->errorInfo(); 
  79.       throw new \Exception($errorInfo[0].'###'.$errorInfo[1].'###'.$errorInfo[2]); 
  80.     } 
  81.     $id = 0; 
  82.     $myPDOStatement->bindParam(":id",$id); 
  83.     $myPDOStatement->execute(); 
  84.     if($myPDOStatement->errorCode()>0){ 
  85.       $errorInfo = $myPDOStatement->errorInfo(); 
  86.       throw new \Exception($errorInfo[0].'###'.$errorInfo[1].'###'.$errorInfo[2]); 
  87.     } 
  88.     $list = $myPDOStatement->fetchAll(); 
  89.     print_r($list); 
  90.   } 
  91.   if($update = true){ 
  92.     $myPDOStatement = $pdo->prepare("DELETE FROM " . $tableName . " WHERE id=:id"); 
  93.     if(!$myPDOStatement) { 
  94.       $errorInfo = $myPDOStatement->errorInfo(); 
  95.       throw new \Exception($errorInfo[0].'###'.$errorInfo[1].'###'.$errorInfo[2]); 
  96.     } 
  97.     //$insertedId = 10; 
  98.     $myPDOStatement->bindParam(":id",$insertedId); 
  99.     $myPDOStatement->execute(); 
  100.     if($myPDOStatement->errorCode()>0){ 
  101.       $errorInfo = $myPDOStatement->errorInfo(); 
  102.       throw new \Exception($errorInfo[0].'###'.$errorInfo[1].'###'.$errorInfo[2]); 
  103.     } 
  104.     $affectRowCount = $myPDOStatement->rowCount(); 
  105.     print_r('$affectRowCount = '.$affectRowCount); 
  106.   } 
  107.   $pdo->commit(); 
  108. } catch (\Exception $e) { 
  109.   $pdo->rollBack(); 
  110. //     print_r($e); 
  111. } 
  112. $pdo = null;

Tags: PDO PostgreSQL

分享到: