- PostgreSQL 教程
- PostgreSQL - 首頁
- PostgreSQL - 概述
- PostgreSQL - 環境設定
- PostgreSQL - 語法
- PostgreSQL - 資料型別
- PostgreSQL - 建立資料庫
- PostgreSQL - 選擇資料庫
- PostgreSQL - 刪除資料庫
- PostgreSQL - 建立表
- PostgreSQL - 刪除表
- PostgreSQL - 模式
- PostgreSQL - 插入查詢
- PostgreSQL - 選擇查詢
- PostgreSQL - 運算子
- PostgreSQL - 表示式
- PostgreSQL - WHERE 子句
- PostgreSQL - AND & OR 子句
- PostgreSQL - 更新查詢
- PostgreSQL - 刪除查詢
- PostgreSQL - LIKE 子句
- PostgreSQL - LIMIT 子句
- PostgreSQL - ORDER BY 子句
- PostgreSQL - GROUP BY
- PostgreSQL - WITH 子句
- PostgreSQL - HAVING 子句
- PostgreSQL - DISTINCT 關鍵字
- 高階 PostgreSQL
- PostgreSQL - 約束
- PostgreSQL - 連線
- PostgreSQL - UNION 子句
- PostgreSQL - NULL 值
- PostgreSQL - 別名語法
- PostgreSQL - 觸發器
- PostgreSQL - 索引
- PostgreSQL - ALTER TABLE 命令
- 截斷表命令
- PostgreSQL - 檢視
- PostgreSQL - 事務
- PostgreSQL - 鎖
- PostgreSQL - 子查詢
- PostgreSQL - 自動遞增
- PostgreSQL - 許可權
- 日期/時間函式和運算子
- PostgreSQL - 函式
- PostgreSQL - 有用函式
- PostgreSQL 介面
- PostgreSQL - C/C++
- PostgreSQL - Java
- PostgreSQL - PHP
- PostgreSQL - Perl
- PostgreSQL - Python
- PostgreSQL 有用資源
- PostgreSQL - 快速指南
- PostgreSQL - 有用資源
- PostgreSQL - 討論
PostgreSQL - PHP 介面
安裝
在最新版本的 PHP 5.3.x 中,PostgreSQL 擴充套件預設啟用。可以透過在編譯時使用 **--without-pgsql** 來停用它。您仍然可以使用 yum 命令來安裝 PHP -PostgreSQL 介面 -
yum install php-pgsql
在開始使用 PHP PostgreSQL 介面之前,請在 PostgreSQL 安裝目錄中找到 **pg_hba.conf** 檔案並新增以下行 -
# IPv4 local connections: host all all 127.0.0.1/32 md5
如果 postgres 伺服器未執行,您可以使用以下命令啟動/重啟它 -
[root@host]# service postgresql restart Stopping postgresql service: [ OK ] Starting postgresql service: [ OK ]
Windows 使用者必須啟用 php_pgsql.dll 才能使用此擴充套件。此 DLL 包含在最新版本的 PHP 5.3.x 的 Windows 發行版中。
有關詳細的安裝說明,請檢視我們的 PHP 教程及其官方網站。
PHP 介面 API
以下是重要的 PHP 例程,可以滿足您從 PHP 程式中使用 PostgreSQL 資料庫的要求。如果您正在尋找更復雜的應用程式,那麼您可以檢視 PHP 官方文件。
| 序號 | API & 描述 |
|---|---|
| 1 | resource pg_connect ( string $connection_string [, int $connect_type ] ) 此函式開啟到由 connection_string 指定的 PostgreSQL 資料庫的連線。 如果將 PGSQL_CONNECT_FORCE_NEW 作為 connect_type 傳遞,則即使 connection_string 與現有連線相同,在第二次呼叫 pg_connect() 時也會建立一個新連線。 |
| 2 | bool pg_connection_reset ( resource $connection ) 此例程重置連線。它對錯誤恢復很有用。成功時返回 TRUE,失敗時返回 FALSE。 |
| 3 | int pg_connection_status ( resource $connection ) 此例程返回指定連線的狀態。返回 PGSQL_CONNECTION_OK 或 PGSQL_CONNECTION_BAD。 |
| 4 | string pg_dbname ([ resource $connection ] ) 此例程返回給定 PostgreSQL 連線資源的資料庫名稱。 |
| 5 | resource pg_prepare ([ resource $connection ], string $stmtname, string $query ) 此函式提交建立具有給定引數的預處理語句的請求並等待完成。 |
| 6 | resource pg_execute ([ resource $connection ], string $stmtname, array $params ) 此例程傳送執行具有給定引數的預處理語句的請求並等待結果。 |
| 7 | resource pg_query ([ resource $connection ], string $query ) 此例程在指定的資料庫連線上執行查詢。 |
| 8 | array pg_fetch_row ( resource $result [, int $row ] ) 此例程從與指定結果資源關聯的結果中獲取一行資料。 |
| 9 | array pg_fetch_all ( resource $result ) 此例程返回一個數組,其中包含結果資源中的所有行(記錄)。 |
| 10 | int pg_affected_rows ( resource $result ) 此例程返回 INSERT、UPDATE 和 DELETE 查詢影響的行數。 |
| 11 | int pg_num_rows ( resource $result ) 此例程返回 PostgreSQL 結果資源中的行數,例如 SELECT 語句返回的行數。 |
| 12 | bool pg_close ([ resource $connection ] ) 此例程關閉與給定連線資源關聯的 PostgreSQL 資料庫的非持久連線。 |
| 13 | string pg_last_error ([ resource $connection ] ) 此例程返回給定連線的最後一條錯誤訊息。 |
| 14 | string pg_escape_literal ([ resource $connection ], string $data ) 此例程轉義要插入文字欄位的文字。 |
| 15 | string pg_escape_string ([ resource $connection ], string $data ) 此例程轉義用於查詢資料庫的字串。 |
連線到資料庫
以下 PHP 程式碼顯示如何連線到本地機器上的現有資料庫,最後將返回資料庫連線物件。
<?php
$host = "host = 127.0.0.1";
$port = "port = 5432";
$dbname = "dbname = testdb";
$credentials = "user = postgres password=pass123";
$db = pg_connect( "$host $port $dbname $credentials" );
if(!$db) {
echo "Error : Unable to open database\n";
} else {
echo "Opened database successfully\n";
}
?>
現在,讓我們執行上面給出的程式來開啟我們的資料庫 **testdb**:如果資料庫成功開啟,則會顯示以下訊息 -
Opened database successfully
建立表
以下 PHP 程式將用於在先前建立的資料庫中建立表 -
<?php
$host = "host = 127.0.0.1";
$port = "port = 5432";
$dbname = "dbname = testdb";
$credentials = "user = postgres password=pass123";
$db = pg_connect( "$host $port $dbname $credentials" );
if(!$db) {
echo "Error : Unable to open database\n";
} else {
echo "Opened database successfully\n";
}
$sql =<<<EOF
CREATE TABLE COMPANY
(ID INT PRIMARY KEY NOT NULL,
NAME TEXT NOT NULL,
AGE INT NOT NULL,
ADDRESS CHAR(50),
SALARY REAL);
EOF;
$ret = pg_query($db, $sql);
if(!$ret) {
echo pg_last_error($db);
} else {
echo "Table created successfully\n";
}
pg_close($db);
?>
執行上面給出的程式時,它將在您的 **testdb** 中建立 COMPANY 表,並顯示以下訊息 -
Opened database successfully Table created successfully
INSERT 操作
以下 PHP 程式顯示瞭如何在上面示例中建立的 COMPANY 表中建立記錄 -
<?php
$host = "host=127.0.0.1";
$port = "port=5432";
$dbname = "dbname = testdb";
$credentials = "user = postgres password=pass123";
$db = pg_connect( "$host $port $dbname $credentials" );
if(!$db) {
echo "Error : Unable to open database\n";
} else {
echo "Opened database successfully\n";
}
$sql =<<<EOF
INSERT INTO COMPANY (ID,NAME,AGE,ADDRESS,SALARY)
VALUES (1, 'Paul', 32, 'California', 20000.00 );
INSERT INTO COMPANY (ID,NAME,AGE,ADDRESS,SALARY)
VALUES (2, 'Allen', 25, 'Texas', 15000.00 );
INSERT INTO COMPANY (ID,NAME,AGE,ADDRESS,SALARY)
VALUES (3, 'Teddy', 23, 'Norway', 20000.00 );
INSERT INTO COMPANY (ID,NAME,AGE,ADDRESS,SALARY)
VALUES (4, 'Mark', 25, 'Rich-Mond ', 65000.00 );
EOF;
$ret = pg_query($db, $sql);
if(!$ret) {
echo pg_last_error($db);
} else {
echo "Records created successfully\n";
}
pg_close($db);
?>
執行上面給出的程式時,它將在 COMPANY 表中建立給定的記錄,並顯示以下兩行 -
Opened database successfully Records created successfully
SELECT 操作
以下 PHP 程式顯示瞭如何從上面示例中建立的 COMPANY 表中獲取和顯示記錄 -
<?php
$host = "host = 127.0.0.1";
$port = "port = 5432";
$dbname = "dbname = testdb";
$credentials = "user = postgres password=pass123";
$db = pg_connect( "$host $port $dbname $credentials" );
if(!$db) {
echo "Error : Unable to open database\n";
} else {
echo "Opened database successfully\n";
}
$sql =<<<EOF
SELECT * from COMPANY;
EOF;
$ret = pg_query($db, $sql);
if(!$ret) {
echo pg_last_error($db);
exit;
}
while($row = pg_fetch_row($ret)) {
echo "ID = ". $row[0] . "\n";
echo "NAME = ". $row[1] ."\n";
echo "ADDRESS = ". $row[2] ."\n";
echo "SALARY = ".$row[4] ."\n\n";
}
echo "Operation done successfully\n";
pg_close($db);
?>
執行上面給出的程式時,將產生以下結果。請注意,欄位按建立表時使用的順序返回。
Opened database successfully ID = 1 NAME = Paul ADDRESS = California SALARY = 20000 ID = 2 NAME = Allen ADDRESS = Texas SALARY = 15000 ID = 3 NAME = Teddy ADDRESS = Norway SALARY = 20000 ID = 4 NAME = Mark ADDRESS = Rich-Mond SALARY = 65000 Operation done successfully
UPDATE 操作
以下 PHP 程式碼顯示瞭如何使用 UPDATE 語句更新任何記錄,然後從我們的 COMPANY 表中獲取和顯示更新後的記錄 -
<?php
$host = "host=127.0.0.1";
$port = "port=5432";
$dbname = "dbname = testdb";
$credentials = "user = postgres password=pass123";
$db = pg_connect( "$host $port $dbname $credentials" );
if(!$db) {
echo "Error : Unable to open database\n";
} else {
echo "Opened database successfully\n";
}
$sql =<<<EOF
UPDATE COMPANY set SALARY = 25000.00 where ID=1;
EOF;
$ret = pg_query($db, $sql);
if(!$ret) {
echo pg_last_error($db);
exit;
} else {
echo "Record updated successfully\n";
}
$sql =<<<EOF
SELECT * from COMPANY;
EOF;
$ret = pg_query($db, $sql);
if(!$ret) {
echo pg_last_error($db);
exit;
}
while($row = pg_fetch_row($ret)) {
echo "ID = ". $row[0] . "\n";
echo "NAME = ". $row[1] ."\n";
echo "ADDRESS = ". $row[2] ."\n";
echo "SALARY = ".$row[4] ."\n\n";
}
echo "Operation done successfully\n";
pg_close($db);
?>
執行上面給出的程式時,將產生以下結果 -
Opened database successfully Record updated successfully ID = 2 NAME = Allen ADDRESS = 25 SALARY = 15000 ID = 3 NAME = Teddy ADDRESS = 23 SALARY = 20000 ID = 4 NAME = Mark ADDRESS = 25 SALARY = 65000 ID = 1 NAME = Paul ADDRESS = 32 SALARY = 25000 Operation done successfully
DELETE 操作
以下 PHP 程式碼顯示瞭如何使用 DELETE 語句刪除任何記錄,然後從我們的 COMPANY 表中獲取和顯示剩餘的記錄 -
<?php
$host = "host = 127.0.0.1";
$port = "port = 5432";
$dbname = "dbname = testdb";
$credentials = "user = postgres password=pass123";
$db = pg_connect( "$host $port $dbname $credentials" );
if(!$db) {
echo "Error : Unable to open database\n";
} else {
echo "Opened database successfully\n";
}
$sql =<<<EOF
DELETE from COMPANY where ID=2;
EOF;
$ret = pg_query($db, $sql);
if(!$ret) {
echo pg_last_error($db);
exit;
} else {
echo "Record deleted successfully\n";
}
$sql =<<<EOF
SELECT * from COMPANY;
EOF;
$ret = pg_query($db, $sql);
if(!$ret) {
echo pg_last_error($db);
exit;
}
while($row = pg_fetch_row($ret)) {
echo "ID = ". $row[0] . "\n";
echo "NAME = ". $row[1] ."\n";
echo "ADDRESS = ". $row[2] ."\n";
echo "SALARY = ".$row[4] ."\n\n";
}
echo "Operation done successfully\n";
pg_close($db);
?>
執行上面給出的程式時,將產生以下結果 -
Opened database successfully Record deleted successfully ID = 3 NAME = Teddy ADDRESS = 23 SALARY = 20000 ID = 4 NAME = Mark ADDRESS = 25 SALARY = 65000 ID = 1 NAME = Paul ADDRESS = 32 SALARY = 25000 Operation done successfully