本文實例講述了PHP連接及操作PostgreSQL數(shù)據(jù)庫的方法。分享給大家供大家參考,具體如下:
PostgreSQL擴展在默認(rèn)情況下在最新版本的PHP 5.3.x中是啟用的。 可以在編譯時使用--without-pgsql來禁用它。仍然可以使用yum命令來安裝PHP-PostgreSQL接口:
在開始使用PHP連接PostgreSQL接口之前,請先在PostgreSQL安裝目錄中找到pg_hba.conf文件,并添加以下行:
# IPv4 local connections:
host all all 127.0.0.1/32 md5
您可以啟動/重新啟動postgres服務(wù)器,使用以下命令運行:
[root@host]# service postgresql restart
Stopping postgresql service: [ OK ]
Starting postgresql service: [ OK ]
Windows用戶必須啟用php_pgsql.dll才能使用此擴展名。這個DLL包含在最新版本的PHP 5.3.x中的Windows發(fā)行版中。
PHP連接到PostgreSQL數(shù)據(jù)庫
以下PHP代碼顯示如何連接到本地機器上的現(xiàn)有數(shù)據(jù)庫,最后將返回數(shù)據(jù)庫連接對象。
?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)在,讓我們運行上面的程序打開數(shù)據(jù)庫:testdb,如果成功打開數(shù)據(jù)庫連接,那么它將給出以下消息:
Opened database successfully
創(chuàng)建表
以下PHP程序?qū)⒂糜谠谥皠?chuàng)建的數(shù)據(jù)庫(testdb)中創(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í)行上述程序時,它將在testdb數(shù)據(jù)庫中創(chuàng)建COMPANY表,并顯示以下消息:
Opened database successfully
Table created successfully
SQL
插入操作
以下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í)行上述程序時,它將在COMPANY表中創(chuàng)建給定的記錄,并顯示以下兩行:
Opened database successfully
Records created successfully
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í)行上述程序時,將產(chǎn)生以下結(jié)果。 請記下,在創(chuàng)建表時按照它們使用的順序返回字段。
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語句來更新指定記錄,然后從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í)行上述程序時,會產(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語句刪除指定記錄,然后從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í)行上述程序時,會產(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
更多關(guān)于PHP相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《PHP基于pdo操作數(shù)據(jù)庫技巧總結(jié)》、《php+Oracle數(shù)據(jù)庫程序設(shè)計技巧總結(jié)》、《PHP+MongoDB數(shù)據(jù)庫操作技巧大全》、《php面向?qū)ο蟪绦蛟O(shè)計入門教程》、《php字符串(string)用法總結(jié)》、《php+mysql數(shù)據(jù)庫操作入門教程》及《php常見數(shù)據(jù)庫操作技巧匯總》
希望本文所述對大家PHP程序設(shè)計有所幫助。
您可能感興趣的文章:- python連接PostgreSQL數(shù)據(jù)庫的過程詳解
- C# 操作PostgreSQL 數(shù)據(jù)庫的示例代碼
- 在Ubuntu中安裝Postgresql數(shù)據(jù)庫的步驟詳解
- Linux下創(chuàng)建Postgresql數(shù)據(jù)庫的方法步驟
- postgresql 利用fdw來實現(xiàn)不同數(shù)據(jù)庫之間數(shù)據(jù)互通(推薦)