Home > Backend Development > PHP Tutorial > php uses PDO extension to connect to PostgreSQL object relational database

php uses PDO extension to connect to PostgreSQL object relational database

WBOY
Release: 2016-07-29 09:12:04
Original
1662 people have browsed it

php uses PDO extension to connect to PostgreSQL object relational database

$pdo = NULL;
		if(<strong>version_compare</strong>(PHP_VERSION, '5.3.6', '<&#39;)){
			$pdo = new \PDO(&#39;pgsql:host=127.0.0.1;port=5432;dbname=postgredb1&#39;,&#39;postgres&#39;,"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 \<strong>Exception</strong>($errorInfo[0].'###'.$errorInfo[1].'###'.$errorInfo[2]);
				}
				$id = 1;
				$myPDOStatement->bindParam(":id",$id);
				$myPDOStatement->execute();
				if($myPDOStatement->errorCode()>0){
					$errorInfo = $myPDOStatement->errorInfo();
					throw new \<strong>Exception</strong>($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 \<strong>Exception</strong>($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 \<strong>Exception</strong>($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 \<strong>Exception</strong>($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 \<strong>Exception</strong>($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 \<strong>Exception</strong>($errorInfo[0].'###'.$errorInfo[1].'###'.$errorInfo[2]);
				}
				$id = 0;
				$myPDOStatement->bindParam(":id",$id);
				$myPDOStatement->execute();
				if($myPDOStatement->errorCode()>0){
					$errorInfo = $myPDOStatement->errorInfo();
					throw new \<strong>Exception</strong>($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 \<strong>Exception</strong>($errorInfo[0].'###'.$errorInfo[1].'###'.$errorInfo[2]);
				}
				//$insertedId = 10;
				$myPDOStatement->bindParam(":id",$insertedId);
				$myPDOStatement->execute();
				if($myPDOStatement->errorCode()>0){
					$errorInfo = $myPDOStatement->errorInfo();
					throw new \<strong>Exception</strong>($errorInfo[0].'###'.$errorInfo[1].'###'.$errorInfo[2]);
				}
				$affectRowCount = $myPDOStatement->rowCount();
				print_r('$affectRowCount = '.$affectRowCount);
			}
			$pdo->commit();
		} catch (\<strong>Exception</strong> $e) {
			$pdo->rollBack();
// 			print_r($e);
		}
		$pdo = null;
Copy after login

The above introduces how PDO uses the PDO extension to connect to the PostgreSQL object-relational database, including version_compare and Exception. I hope it will be helpful to friends who are interested in PHP tutorials.

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template