1. PHP 連接 PostgreSQL 數(shù)據(jù)庫(kù)
PostgreSQL 擴(kuò)展在默認(rèn)情況下在最新版本的 <strong style="color: red;">PHP 5.3.x
</strong> 中是啟用的雷绢。 可以在編譯時(shí)使用<strong style="color: red;">--without-pgsql
</strong>來(lái)禁用它。仍然可以使用<strong style="color: red;">yum
</strong>命令來(lái)安裝 PHP-PostgreSQL
接口:
yum install php-pgsql
在開(kāi)始使用 PHP 連接 PostgreSQL 接口之前,請(qǐng)先在 PostgreSQL 安裝目錄中找到pg_hba.conf
文件沧烈,并添加以下行:
# IPv4 local connections:
host all all 127.0.0.1/32 md5
您可以啟動(dòng)/重新啟動(dòng) postgres 服務(wù)器默勾,使用以下命令運(yùn)行:
[root@host]# service postgresql restart
Stopping postgresql service: [ OK ]
Starting postgresql service: [ OK ]
Windows 用戶必須啟用 php_pgsql.dll
才能使用此擴(kuò)展名棘幸。這個(gè) DLL 包含在最新版本的 PHP 5.3.x
中的 Windows 發(fā)行版中撒强。
2. PHP 連接到 PostgreSQL 數(shù)據(jù)庫(kù)
以下 PHP 代碼顯示如何連接到本地機(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)行上面的程序打開(kāi)數(shù)據(jù)庫(kù): testdb
,如果成功打開(kāi)數(shù)據(jù)庫(kù)連接,那么它將給出以下消息:
Opened database successfully
3. 創(chuàng)建表
以下 PHP 程序?qū)⒂糜谠谥皠?chuàng)建的數(shù)據(jù)庫(kù)(testdb)中創(chuàng)建一個(gè)表:
<?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
數(shù)據(jù)庫(kù)中創(chuàng)建 COMPANY 表蓝厌,并顯示以下消息:
Opened database successfully
Table created successfully
4. 插入操作
以下 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
5. SELECT 操作
以下 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
6. 更新操作
以下 PHP 代碼顯示了如何使用 UPDATE 語(yǔ)句來(lái)更新指定記錄,然后從 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);
執(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
7. 刪除操作
以下 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);
執(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