1. 云栖社区>
  2. PHP教程>
  3. 正文

php使用PDO扩展连接PostgreSQL对象关系数据库

作者:用户 来源:互联网 时间:2017-12-01 16:51:18

数据库对象

php使用PDO扩展连接PostgreSQL对象关系数据库 - 摘要: 本文讲的是php使用PDO扩展连接PostgreSQL对象关系数据库, php使用PDO扩展连接PostgreSQL对象关系数据库,有需要的朋友可以参考下。 php使用PDO扩展连接PostgreSQL对象关系数据库 $pdo = NULL; if(version_compare(PHP_VERSION,

php使用PDO扩展连接PostgreSQL对象关系数据库,有需要的朋友可以参考下。


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;


版权声明:本文为博主原创文章,未经博主允许不得转载。

以上是云栖社区小编为您精心准备的的内容,在云栖社区的博客、问答、公众号、人物、课程等栏目也有的相关内容,欢迎继续使用右上角搜索按钮进行搜索数据库 对象 ,以便于您获取更多的相关知识。

弹性可伸缩的计算服务,助您降低 IT 成本,提升运维效率

40+云计算产品,6个月免费体验

现在注册,免费体验40+云产品,及域名优惠!

云服务器9.9元/月,大学必备