PostgreSQL PHP接口

PostgreSQL PHP接口

安装

PostgreSQL扩展在最新版本的PHP 5.3.x中默认启用。您可以在编译时使用 --without-pgsql 来禁用它。但您仍然可以使用yum命令来安装PHP-PostgreSQL接口。

yum install php-pgsql

在开始使用PHP PostgreSQL接口之前,请在您的PostgreSQL安装目录中找到 pg_hba.conf 文件,并添加以下行 –

# IPv4 local connections:
host    all         all         127.0.0.1/32          md5

如果PostgreSQL服务器没有运行,您可以使用以下命令启动/重启该服务器−

[root@host]# service postgresql restart
Stopping postgresql service:                               [  OK  ]
Starting postgresql service:                               [  OK  ]

Windows用户必须启用php_pgsql.dll才能使用此扩展。该DLL包含在Windows发行版中的最新版本的PHP 5.3.x中。

有关详细的安装说明,请查看我们的PHP教程和官方网站。

PHP接口API

以下是重要的PHP例程,它们可以满足您在PHP程序中使用PostgreSQL数据库的需求。如果您正在寻找更复杂的应用程序,请参阅PHP官方文档。

序号 API 和描述
1 resource pg_connect ( string connection_string [, intconnect_type ] ) 此函数打开与由 connection_string 指定的 PostgreSQL 数据库的连接。 如果 connect_type 为 PGSQL_CONNECT_FORCE_NEW,那么在第二次调用 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 ], stringstmtname, string $query ) 此例程提交一个带有给定参数的请求,用来创建一个准备好的语句,并等待完成。
6 resource pg_execute ([ resource connection ], stringstmtname, array $params ) 此例程发送一个带有给定参数的请求,用来执行一个准备好的语句,并等待结果。
7 resource pg_query ([ resource connection ], stringquery ) 此例程在指定的数据库连接上执行查询。
8 array pg_fetch_row ( resource result [, introw ] ) 此例程从与指定结果资源关联的结果中获取一行数据。
9 array pg_fetch_all ( resource $result ) 此例程返回一个包含结果资源中所有行(记录)的数组。
10 int pg_affected_rows ( resource $result ) 此例程返回INSERT、UPDATE和DELETE查询影响的行数。
11 int pg_num_rows ( resource $result ) 这个函数返回一个PostgreSQL结果资源中的行数,例如SELECT语句返回的行数。
12 bool pg_close ([ resource $connection ] ) 这个函数关闭与给定连接资源关联的非持久连接到PostgreSQL数据库。
13 string pg_last_error ([ resource $connection ] ) 这个函数返回给定连接的最后一个错误消息。
14 string pg_escape_literal ([ resource connection ], stringdata ) 这个函数用于转义要插入到文本字段中的文字。
15 string pg_escape_string ([ resource connection ], stringdata ) 此例程用于转义查询数据库的字符串。

连接到数据库

以下PHP代码展示了如何连接到本地机器上的现有数据库,并最终返回一个数据库连接对象。

<?php
   host        = "host = 127.0.0.1";port        = "port = 5432";
   dbname      = "dbname = testdb";credentials = "user = postgres password=pass123";

   db = pg_connect( "host portdbname 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 portdbname 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);
?>

当执行上面给定的程序时,它将在您的testdb中创建一个COMPANY表,并显示以下信息:

Opened database successfully
Table created successfully

插入操作

下面的PHP程序展示了如何在上面示例创建的COMPANY表中插入记录 –

<?php
   host        = "host=127.0.0.1";port        = "port=5432";
   dbname      = "dbname = testdb";credentials = "user = postgres password=pass123";

   db = pg_connect( "host portdbname 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

选择操作

以下的PHP程序展示了我们如何从上面的例子中创建的COMPANY表中提取和显示记录:

<?php
   host        = "host = 127.0.0.1";port        = "port = 5432";
   dbname      = "dbname = testdb";credentials = "user = postgres password=pass123";

   db = pg_connect( "host portdbname 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 portdbname 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

删除操作

以下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 portdbname 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

Python教程

Java教程

Web教程

数据库教程

图形图像教程

大数据教程

开发工具教程

计算机教程