php_pdo_pgsql 接口
1.簡介
PHP* *數(shù)據(jù)對象(PDO) 擴(kuò)展為PHP訪問數(shù)據(jù)庫定義了一個輕量級的一致接口。實現(xiàn) PDO 接口的每個數(shù)據(jù)庫驅(qū)動可以將特定具體數(shù)據(jù)庫的特性公開作為標(biāo)準(zhǔn)擴(kuò)展函數(shù)。 注意不能單獨使用 PDO 擴(kuò)展執(zhí)行任何數(shù)據(jù)庫功能;必須使用一個具體數(shù)據(jù)庫的 PDO 驅(qū)動程序來訪問數(shù)據(jù)庫服務(wù)。
PDO 提供了數(shù)據(jù)訪問抽象層,這意味著,不管使用哪種數(shù)據(jù)庫,都可以用相同的函數(shù)(方法)來查詢和獲取數(shù)據(jù)。 PDO 不提供數(shù)據(jù)庫抽象;它不會重寫 SQL,也不會模擬缺失的特性。如果需要的話,應(yīng)該使用一個成熟的抽象層。
從 PHP 5.1 開始附帶了 PDO,在 PHP 5.0 中是作為一個 PECL 擴(kuò)展使用。 PDO 需要PHP 5 核心的新 OO 特性,因此不能在較早版本的 PHP 上運行。
HighGo 數(shù)據(jù)庫提供了對 PDO 特性的支持,并且支持連接認(rèn)證方式為國密 SM3。
2.下載配置PHP 與 瀚高驅(qū)動
2.1.下載地址
鏈接:https://pan.baidu.com/s/1xuz6uJz0utRgKWecXhpOiA?pwd=o0tj
2.2.Windows 環(huán)境
- 前提條件,確保已安裝php對應(yīng)版本的 php_pdo_pgsql.dll 擴(kuò)展。
- 下載對應(yīng)版本的libpq.dll,上傳位置為 PHP 安裝根目錄。
- 修改php.ini,去掉“extension=pdo_pgsql”前的分號。
- 終端執(zhí)行 php -m ,查看輸出列表是否有“pdo_pgsql”,如果有表示加載成功,沒有表示加載失敗
2.3.Linux 環(huán)境
- 下載對應(yīng)版本驅(qū)動
- *上傳服務(wù)器 /opt/highgolib 并解壓 *
tar -zxvf v458_driver_for_php-7.4.33_x86_64.tar.gz
|
- 修改php.ini,引入擴(kuò)展
Textextension= /opt/highgolib/pdo_pgsql.so
|
- 終端執(zhí)行 php -m ,查看輸出列表是否有“pdo_pgsql”,如果有表示加載成功,沒有表示加載失敗
3.數(shù)據(jù)庫操作
pdo_pgsql_test.php
<?php
$dbms='pgsql'; $host='192.168.100.101'; $dbName='pdodb'; $port=5866; $user='sysdba'; $pass='Qwer@1234'; $dsn="$dbms:host=$host;dbname=$dbName;port=$port";
try { $pdo = new PDO($dsn, $user, $pass); $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); echo "成功連接到數(shù)據(jù)庫服務(wù)器!<br>";
} catch (PDOException $e) { echo ("Error!: " . $e->getMessage() . "<br>"); }
try { $create_table_sql = "create table if not exists student( id serial primary key, name varchar(100), age int, birthday timestamp )"; $create_table_result = $pdo->exec($create_table_sql); echo "Table created successfully :" . $create_table_result . " <br>"; $insert_sql = 'INSERT INTO student(name, age, birthday) VALUES(:name, :age, :birthday)'; $stmt = $pdo->prepare($insert_sql); $name = 'tony'; $age=19; $birthday = '2015-01-01 20:20:20'; $stmt->bindValue(':name', $name); $stmt->bindValue(':age', $age); $stmt->bindValue(':birthday', $birthday);
$stmt->execute();
$id = $pdo->lastInsertId('student_id_seq'); echo '插入數(shù)據(jù)成功,用戶id:' . $id . '<br>'; $stmt = $pdo->query('SELECT * FROM student'); $students = []; while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) { $students[] = [ 'id' => $row['id'], 'name' => $row['name'], 'age' => $row['age'], 'birthday' => $row['birthday'] ]; }
} catch (PDOException $e) { echo ("Error!: " . $e->getMessage() . "<br>"); }
?>
|