PDO擴充連接PostgreSQL物件關聯式資料庫步驟詳解
這次帶給大家PDO擴充連接PostgreSQL物件關係資料庫步驟詳解,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物件關聯式資料庫步驟詳解的詳細內容。更多資訊請關注PHP中文網其他相關文章!

熱AI工具

Undresser.AI Undress
人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

AI Hentai Generator
免費產生 AI 無盡。

熱門文章

熱工具

記事本++7.3.1
好用且免費的程式碼編輯器

SublimeText3漢化版
中文版,非常好用

禪工作室 13.0.1
強大的PHP整合開發環境

Dreamweaver CS6
視覺化網頁開發工具

SublimeText3 Mac版
神級程式碼編輯軟體(SublimeText3)

熱門話題

JSON(JavaScriptObjectNotation)是一種輕量級的資料交換格式,已成為Web應用程式之間資料交換的常用格式。 PHP的json_encode()函數可以將陣列或物件轉換為JSON字串。本文將介紹如何使用PHP的json_encode()函數,包括語法、參數、傳回值以及具體的範例。語法json_encode()函數的語法如下:st

將MySQL查詢結果陣列轉換為物件的方法如下:建立一個空物件陣列。循環結果數組並為每一行建立一個新的物件。使用foreach迴圈將每一行的鍵值對賦給新物件的對應屬性。將新物件加入到物件數組中。關閉資料庫連線。

使用Python的__contains__()函數定義物件的包含操作Python是一種簡潔而強大的程式語言,提供了許多強大的功能來處理各種類型的資料。其中之一是透過定義__contains__()函數來實現物件的包含操作。本文將介紹如何使用__contains__()函數來定義物件的包含操作,並且給予一些範例程式碼。 __contains__()函數是Pytho

MySQL和PostgreSQL:在Web開發中的最佳實務引言:在現代的Web開發領域中,資料庫是不可或缺的組成部分。在選擇資料庫時,常見的選擇是MySQL和PostgreSQL。本文將介紹在Web開發中使用MySQL和PostgreSQL的最佳實踐,並提供一些程式碼範例。一、適用場景MySQL適用於大多數Web應用程序,特別是那些需要高性能、可擴展性和易於使

MySQL與PostgreSQL:效能比較與最佳化技巧在開發web應用程式時,資料庫是不可或缺的組成部分。而在選擇資料庫管理系統時,MySQL和PostgreSQL是兩個常見的選擇。他們都是開源的關係型資料庫管理系統(RDBMS),但在效能和最佳化方面有一些不同之處。本文將比較MySQL和PostgreSQL的效能,並提供一些最佳化技巧。性能對比在比較兩個資料庫管

學習Go語言中的資料庫函數並實作PostgreSQL資料的增刪改查操作在現代的軟體開發中,資料庫是不可或缺的一部分。 Go語言作為一門強大的程式語言,提供了豐富的資料庫操作函數和工具包,可以輕鬆實現資料庫的增刪改查操作。本文將介紹如何學習Go語言中的資料庫函數,並使用PostgreSQL資料庫進行實際的操作。第一步:安裝資料庫驅動程式在Go語言中,每個資料庫

PHP中,數組是有序序列,以索引存取元素;物件是具有屬性和方法的實體,透過new關鍵字建立。數組存取透過索引,物件存取通過屬性/方法。數組值傳遞,物件參考傳遞。

MySQL與PostgreSQL:資料安全與備份策略引言:在現代社會中,資料成為了企業和個人生活中不可或缺的一部分。對於資料庫管理系統來說,資料安全與備份策略是至關重要的,既能保護資料免受遺失或損壞,也能確保恢復資料的可靠性和完整性。本文將重點放在MySQL和PostgreSQL兩種主流關係型資料庫系統的資料安全性和備份策略。一、資料安全性方面:(一)用戶權
