php pdo连接psql,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;

希望本文所述对大家PHP程序设计有所帮助。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值