这篇文章主要介绍了PHP实现基于PDO扩展连接PostgreSQL对象关系数据库,结合实例形式分析了php使用pdo连接PostgreSQL并执行简单sql语句的相关操作技巧,需要的朋友可以参考下
本文实例讲述了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相关内容感兴趣的读者可查看本站专题:《PHP基于pdo操作数据库技巧总结》、《php+Oracle数据库程序设计技巧总结》、《PHP+MongoDB数据库操作技巧大全》、《php面向对象程序设计入门教程》、《php字符串(string)用法总结》、《php+mysql数据库操作入门教程》及《php常见数据库操作技巧汇总》
希望本文所述对大家PHP程序设计有所帮助。
沃梦达教程
本文标题为:PHP实现基于PDO扩展连接PostgreSQL对象关系数据库示例
基础教程推荐
猜你喜欢
- PHP中的错误及其处理机制 2023-06-04
- 使用PHP开发留言板功能 2023-03-13
- laravel 解决多库下的DB::transaction()事务失效问题 2023-03-08
- PHP命名空间简单用法示例 2022-12-01
- 在Laravel中实现使用AJAX动态刷新部分页面 2023-03-02
- PHP获取MySQL执行sql语句的查询时间方法 2022-11-09
- php array分组,PHP中array数组的分组排序 2022-08-01
- PHP实现Redis单据锁以及防止并发重复写入 2022-10-12
- laravel ORM关联关系中的 with和whereHas用法 2023-03-02
- thinkphp3.2.3框架动态切换多数据库的方法分析 2023-03-19