PostgreSQL连接PHP

安装

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官方文件。

S.N. API & 描述
1 resource pg_connect ( string $connection_string [, int $connect_type ] )

这将打开一个连接到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 )

这个例程返回指定的连接状态。返回PGSQL_CONNECTION_OK或PGSQL_CONNECTION_BAD。

4 string pg_dbname ([ resource $connection ] )

这个例程返回给定的PostgreSQL数据库连接资源的名称。

5 resource pg_prepare ([ resource $connection ], string $stmtname , string $query )

提交请求以创建具有给定参数的准备好的语句,并等待完成。

6 resource pg_execute ([ resource $connection ], string $stmtname , array $params )

这个程序发送一个请求给定的参数执行准备好的语句,并等待结果。

7 resource pg_query ([ resource $connection ], string $query )

此程序执行查询指定的数据库连接。

8 array pg_fetch_row ( resource $result [, int $row ] )

该例程将指定的结果的资源相关联的,数据从结果中获取一个行。

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 ] )

该例程关闭非持久连接到一个给定的连接资源相关的PostgreSQL数据库。

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
";
   } else {
      echo "Opened database successfully
";
   }
?>

现在让我们运行上面的程序来打开数据库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
";
   } else {
      echo "Opened database successfully
";
   }
   
   $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
";
   }
   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
";
   } else {
      echo "Opened database successfully
";
   }

   $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
";
   }
   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
";
   } else {
      echo "Opened database successfully
";
   }

   $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] . "
";
      echo "NAME = ". $row[1] ."
";
      echo "ADDRESS = ". $row[2] ."
";
      echo "SALARY =  ".$row[4] ."

";
   }
   echo "Operation done successfully
";
   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
";
   } else {
      echo "Opened database successfully
";
   }
   $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
";
   }
   
   $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] . "
";
      echo "NAME = ". $row[1] ."
";
      echo "ADDRESS = ". $row[2] ."
";
      echo "SALARY =  ".$row[4] ."

";
   }
   echo "Operation done successfully
";
   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
";
   } else {
      echo "Opened database successfully
";
   }
   $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
";
   }
   
   $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] . "
";
      echo "NAME = ". $row[1] ."
";
      echo "ADDRESS = ". $row[2] ."
";
      echo "SALARY =  ".$row[4] ."

";
   }
   echo "Operation done successfully
";
   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

    点赞