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

WBOY
Lepaskan: 2016-07-29 09:12:04
asal
1642 orang telah melayarinya

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

$pdo = NULL;
		if(<strong>version_compare</strong>(PHP_VERSION, '5.3.6', ''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;
Salin selepas log masuk

以上就介绍了php使用PDO扩展连接PostgreSQL对象关系数据库,包括了version_compare,Exception方面的内容,希望对PHP教程有兴趣的朋友有所帮助。

Label berkaitan:
sumber:php.cn
Kenyataan Laman Web ini
Kandungan artikel ini disumbangkan secara sukarela oleh netizen, dan hak cipta adalah milik pengarang asal. Laman web ini tidak memikul tanggungjawab undang-undang yang sepadan. Jika anda menemui sebarang kandungan yang disyaki plagiarisme atau pelanggaran, sila hubungi admin@php.cn
Tutorial Popular
Lagi>
Muat turun terkini
Lagi>
kesan web
Kod sumber laman web
Bahan laman web
Templat hujung hadapan