安装
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 ] yiibai.com
Windows用户必须启用PHP_pgsql.dll为了使用这个扩展。这个DLL包含带Windows分布在最新版本PHP5.3.x
有关详细的安装说明,请查看我们的PHP教程和其官方网站上。www.yiibai.com
PHP 接口APIs
以下是重要的PHP程序,可以连接使用Postgresql数据库PHP程序。如果正在寻找一个更复杂的应用程序,那么可以看看到PHP官方文件。www.yiibai.com
S.N. | API & 描述 |
---|---|
1 | resource pg_connect ( string $connection_string [,int $connect_type ] )www.yiibai.com 这将打开一个连接到Postgresql数据库由connection_string指定。 如果PGsql_CONNECT_FORCE_NEW会传递为connect_type,然后在第二个调用的情况下,对pg_connect()创建一个新的连接,即使connection_string一个现有的连接是相同的。 |
2 | bool pg_connection_reset ( resource $connection ) 此例程重置连接。它是有用的错误恢复。成功返回TRUE或失败则返回FALSE。 |
3 | int pg_connection_status ( resource $connection ) |
4 | string pg_dbname ([ resource $connection ] ) |
5 | resource pg_prepare ([ resource $connection ],string $stmtname,string $query ) 提交请求以创建具有给定参数的准备好的语句,并等待完成。 |
6 | resource pg_execute ([ resource $connection ],array $params ) 这个程序发送一个请求给定的参数执行准备好的语句,并等待结果。 |
7 | resource pg_query ([ resource $connection ],string $query ) |
8 | array pg_fetch_row ( resource $result [,int $row ] ) 该例程将指定的结果的资源相关联的,数据从结果中获取一个行。www.yiibai.com |
9 | array pg_fetch_all ( resource $result ) 这个例程返回一个数组,包含所有行(记录),结果资源。 |
10 | int pg_affected_rows ( resource $result ) 这个例程返回INSERT,UPDATE和DELETE查询受到影响的行数。 |
11 | int pg_num_rows ( resource $result ) 这个例程返回的行数,例如SELECT语句返回的行数在一个Postgresql结果资源。 |
12 | bool pg_close ([ resource $connection ] ) |
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 $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在上面的例子中,创建表中的记录:www.yiibai.com
<?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,SALARY) VALUES (2,'Allen',25,'Texas',15000.00 ); INSERT INTO COMPANY (ID,SALARY) VALUES (3,'Teddy',23,'Norway',SALARY) VALUES (4,'Mark','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); ?> www.yiibai.com
上述程序执行时,它会创建COMPANY表中的记录,并会显示以下两行:
Opened database successfully Records created successfully www.yiibai.com
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原文链接:https://www.f2er.com/postgresql/194271.html