最新版本的 PHP 5.3. x 默認(rèn)啟用了 PostgreSQL 擴(kuò)展??梢栽诰幾g時(shí)使用 -- without-pgsql 來(lái)禁用它。您仍然可以使用 yum 命令來(lái)安裝 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服務(wù)器沒(méi)有運(yùn)行,可以使用以下命令啟動(dòng)/重新啟動(dòng)postgres服務(wù)器-
[root@host]# service postgresql restart Stopping postgresql service: [ OK ] Starting postgresql service: [ OK ]
Windows用戶必須啟用php_pgsql.dll為了使用這個(gè)擴(kuò)展。這個(gè)DLL包含在PHP5.3.x之后的Windows發(fā)行版中
要了解詳細(xì)的安裝說(shuō)明,請(qǐng)查看我們的PHP教程及其官方網(wǎng)站。
下面是一些重要的 PHP 例程,可以滿足您使用 PHP 程序中的 PostgreSQL 數(shù)據(jù)庫(kù)的需求。如果您正在尋找一個(gè)更復(fù)雜的應(yīng)用程序,那么您可以查看 PHP 官方文檔。
S. No. | API & 描述 |
---|---|
1 | resource pg_connect ( string $connection_string [, int $connect_type ] ) 這將打開(kāi)一個(gè)連接到connection_string指定的PostgreSQL數(shù)據(jù)庫(kù)。 如果PGSQL_CONNECT_FORCE_NEW作為connect_type傳遞,那么在第二次調(diào)用pg_connect()時(shí)將創(chuàng)建一個(gè)新的連接,即使connection_string與現(xiàn)有連接相同。 |
2 | bool pg_connection_reset ( resource $connection ) 此例程重置連接。對(duì)于錯(cuò)誤恢復(fù)非常有用。成功時(shí)返回 TRUE,失敗時(shí)返回 FALSE。 |
3 | int pg_connection_status ( resource $connection ) 返回指定連接的狀態(tài)。返回PGSQL_CONNECTION_OK或PGSQL_CONNECTION_BAD。 |
4 | string pg_dbname ([ resource $connection ] ) 返回給定 PostgreSQL 連接資源的數(shù)據(jù)庫(kù)的名稱。 |
5 | resource pg_prepare ([ resource $connection ], string $stmtname, string $query ) 這樣提交一個(gè)請(qǐng)求,用給定的 something 創(chuàng)建一個(gè)準(zhǔn)備好的語(yǔ)句,并等待完成。 |
6 | resource pg_execute ([ resource $connection ], string $stmtname, array $params ) 這個(gè)例程發(fā)送一個(gè)請(qǐng)求執(zhí)行一份事先準(zhǔn)備好的聲明中與給定參數(shù),等待結(jié)果。 |
7 | resource pg_query ([ resource $connection ], string $query ) 在指定的數(shù)據(jù)庫(kù)連接上執(zhí)行查詢。 |
8 | array pg_fetch_row ( resource $result [, int $row ] ) 從與指定的結(jié)果資源關(guān)聯(lián)的結(jié)果中獲取一行數(shù)據(jù)。 |
9 | array pg_fetch_all ( resource $result ) 返回一個(gè)數(shù)組,該數(shù)組包含結(jié)果資源中的所有行(記錄)。 |
10 | int pg_affected_rows ( resource $result ) 返回受INSERT、UPDATE和DELETE查詢影響的行數(shù)。 |
11 | int pg_num_rows ( resource $result ) 返回 PostgreSQL 結(jié)果資源中的行數(shù),例如 SELECT 語(yǔ)句返回的行數(shù)。 |
12 | bool pg_close ([ resource $connection ] ) 關(guān)閉與給定連接資源關(guān)聯(lián)的 PostgreSQL 數(shù)據(jù)庫(kù)的非持久連接。 |
13 | string pg_last_error ([ resource $connection ] ) 返回給定連接的最后一條錯(cuò)誤消息。 |
14 | string pg_escape_literal ([ resource $connection ], string $data ) 轉(zhuǎn)義一個(gè)文本以插入到文本字段中。 |
15 | string pg_escape_string ([ resource $connection ], string $data ) 轉(zhuǎn)義用于查詢數(shù)據(jù)庫(kù)的字符串。 |
下面的 PHP 代碼演示如何連接到本地計(jì)算機(jī)上的現(xiàn)有數(shù)據(jù)庫(kù),并最終返回?cái)?shù)據(jù)庫(kù)連接對(duì)象。
<?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"; } ?>
現(xiàn)在,讓我們運(yùn)行上面給定的程序來(lái)打開(kāi)我們的數(shù)據(jù)庫(kù) testdb: 如果數(shù)據(jù)庫(kù)被成功打開(kāi),那么它將給出以下消息-
Opened database successfully
以下PHP程序?qū)⒂糜谠谙惹皠?chuàng)建的數(shù)據(jù)庫(kù)中創(chuàng)建表
<?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); ?>
當(dāng)執(zhí)行上面給定的程序時(shí),它將在 testdb 中創(chuàng)建 COMPANY 表,并顯示以下消息-
Opened database successfully Table created successfully
下面的 PHP 程序演示了如何在上面的示例-中創(chuàng)建的 COMPANY 表中創(chuàng)建記錄
<?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); ?>
當(dāng)執(zhí)行上述給定的程序時(shí),它將在 COMPANY 表中創(chuàng)建給定的記錄,并顯示以下兩行
Opened database successfully Records created successfully
下面的 PHP 程序展示了如何從上面的示例-中創(chuàng)建的 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); ?>
當(dāng)上面給出的程序被執(zhí)行時(shí),它將產(chǎn)生以下結(jié)果。請(qǐng)注意,字段將按照創(chuàng)建表時(shí)使用的順序返回。
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
下面的PHP代碼展示了如何使用UPDATE語(yǔ)句更新任何記錄,然后從我們的公司表中獲取并顯示更新后的記錄
<?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); ?>
當(dāng)上述程序執(zhí)行時(shí),會(huì)產(chǎn)生如下結(jié)果
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
下面的 PHP 代碼展示了如何使用 DELETE 語(yǔ)句刪除任何記錄,然后從 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); ?>
當(dāng)上述程序執(zhí)行時(shí),會(huì)產(chǎn)生如下結(jié)果
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