📅  最后修改于: 2020-11-26 06:42:05             🧑  作者: Mango
最新版本的PHP 5.3.x默认启用PostgreSQL扩展。通过在编译时使用–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
如果未运行postgres服务器,则可以使用以下命令启动/重新启动它:
[root@host]# service postgresql restart
Stopping postgresql service: [ OK ]
Starting postgresql service: [ OK ]
Windows用户必须启用php_pgsql.dll才能使用此扩展名。 Windows发行版的PHP 5.3.x中包含此DLL。
有关详细的安装说明,请查看我们的PHP教程及其官方网站。
以下是重要的PHP例程,可以满足您从PHP程序使用PostgreSQL数据库的要求。如果您正在寻找更复杂的应用程序,则可以查阅PHP官方文档。
S. No. | API & Description |
---|---|
1 |
resource pg_connect ( string $connection_string [, int $connect_type ] ) This opens a connection to a PostgreSQL database specified by the connection_string. If PGSQL_CONNECT_FORCE_NEW is passed as connect_type, then a new connection is created in case of a second call to pg_connect(), even if the connection_string is identical to an existing connection. |
2 |
bool pg_connection_reset ( resource $connection ) This routine resets the connection. It is useful for error recovery. Returns TRUE on success or FALSE on failure. |
3 |
int pg_connection_status ( resource $connection ) This routine returns the status of the specified connection. Returns PGSQL_CONNECTION_OK or PGSQL_CONNECTION_BAD. |
4 |
string pg_dbname ([ resource $connection ] ) This routine returns the name of the database that the given PostgreSQL connection resource. |
5 |
resource pg_prepare ([ resource $connection ], string $stmtname, string $query ) This submits a request to create a prepared statement with the given parameters and waits for completion. |
6 |
resource pg_execute ([ resource $connection ], string $stmtname, array $params ) This routine sends a request to execute a prepared statement with given parameters and waits for the result. |
7 |
resource pg_query ([ resource $connection ], string $query ) This routine executes the query on the specified database connection. |
8 |
array pg_fetch_row ( resource $result [, int $row ] ) This routine fetches one row of data from the result associated with the specified result resource. |
9 |
array pg_fetch_all ( resource $result ) This routine returns an array that contains all rows (records) in the result resource. |
10 |
int pg_affected_rows ( resource $result ) This routine returns the number of rows affected by INSERT, UPDATE, and DELETE queries. |
11 |
int pg_num_rows ( resource $result ) This routine returns the number of rows in a PostgreSQL result resource for example number of rows returned by SELECT statement. |
12 |
bool pg_close ([ resource $connection ] ) This routine closes the non-persistent connection to a PostgreSQL database associated with the given connection resource. |
13 |
string pg_last_error ([ resource $connection ] ) This routine returns the last error message for a given connection. |
14 |
string pg_escape_literal ([ resource $connection ], string $data ) This routine escapes a literal for insertion into a text field. |
15 |
string pg_escape_string ([ resource $connection ], string $data ) This routine escapes a string for querying the database. |
以下PHP代码显示了如何连接到本地计算机上的现有数据库,最后将返回一个数据库连接对象。
现在,让我们运行上述给定的程序以打开数据库testdb :如果数据库成功打开,则它将给出以下消息-
Opened database successfully
以下PHP程序将用于在先前创建的数据库中创建表-
执行上述给定程序后,它将在您的testdb中创建COMPANY表,并将显示以下消息-
Opened database successfully
Table created successfully
以下PHP程序显示了如何在上例中创建的COMPANY表中创建记录-
执行上述给定程序时,它将在COMPANY表中创建给定记录,并显示以下两行-
Opened database successfully
Records created successfully
以下PHP程序说明了如何从上例中创建的COMPANY表中获取并显示记录-
执行上述给定程序时,将产生以下结果。请注意,字段以创建表时使用的顺序返回。
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表中获取并显示更新的记录-
执行上面给定的程序时,将产生以下结果-
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表中获取并显示其余记录-
执行上面给定的程序时,将产生以下结果-
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