安装
PostgreSQL的扩展是默认启用的***版本的PHP5.3.x.但是它可能通过在编译时加上--without-pgsql 选项禁用。但仍然可以使用yum命令安装PHP的PostgreSQL的接口:
- yum install php-pgsql
在开始使用PHP PostgreSQL接口,打开pg_hba.conf文件(在PostgreSQL安装目录),并添加下面一行:
- # IPv4 local connections:
- host all all 127.0.0.1/32 md5
使用start/restart启动Postgres服务器,它没有运行使用下面的命令:
- [root@host]# service postgresql restart
- Stopping postgresql service: [ OK ]
- Starting postgresql service: [ OK ]
Windows用户必须启用php_pgsql.dll为了使用这个扩展。这个DLL包含带Windows分布在***版本PHP5.3.x
有关详细的安装说明,请查看我们的PHP教程和其官方网站上。
PHP 接口APIs
以下是重要的PHP程序,可以连接使用PostgreSQL数据库PHP程序。如果正在寻找一个更复杂的应用程序,那么可以看看到PHP官方文件。
Windows用户必须启用php_pgsql.dll为了使用这个扩展。这个DLL包含带Windows分布在***版本PHP5.3.x
有关详细的安装说明,请查看我们的PHP教程和其官方网站上。
PHP 接口APIs
以下是重要的PHP程序,可以连接使用PostgreSQL数据库PHP程序。如果正在寻找一个更复杂的应用程序,那么可以看看到PHP官方文件。
连接到数据库
下面的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);
- ?>
上述程序执行时,它会创建表COMPANY在数据库testdb 中 ,它会显示以下消息:
- 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
原文链接:http://www.yiibai.com/html/postgresql/2013/080896.html