• 技术文章 >后端开发 >php教程

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

    2016-07-29 09:12:04原创716
    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;

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

    声明:本文原创发布php中文网,转载请注明出处,感谢您的尊重!如有疑问,请联系admin@php.cn处理
    上一篇:CentOS 配置Tomcat及搭配Nginx 下一篇:好的编程人员是好的作家
    大前端线上培训班

    相关文章推荐

    • 带你分清类中的构造函数与析构函数• PHP中的命名空间定义与使用(实例详解)• PHP中clone关键字和__clone()方法的使用(实例详解)• 怎样去搞定PHP类的继承?(总结分享)• 五分钟带你了解PHP中的魔术方法(实例详解)

    全部评论我要评论

  • 取消发布评论发送
  • 1/1

    PHP中文网