首页
学习
活动
专区
工具
TVP
发布
精选内容/技术社群/优惠产品,尽在小程序
立即前往

pg_pconnect

(PHP 4, PHP 5, PHP 7)

pg_pconnect - 打开一个持久的PostgreSQL连接

描述

代码语言:javascript
复制
resource pg_pconnect ( string $connection_string [, int $connect_type ] )

pg_pconnect()打开一个到PostgreSQL数据库的连接。它返回其他PostgreSQL函数所需的连接资源。

如果第二次调用与现有连接相同的pg_pconnect(),则connection_string除非您传递PGSQL_CONNECT_FORCE_NEWas,否则将返回现有连接connect_type

要启用持续连接,必须将pgsql.allow_persistent php.ini指令设置为“On”(这是默认值)。可以使用pgsql.max_persistent php.ini指令定义持久连接的最大数量(默认为-1,无限制)。可以使用pgsql.max_links php.ini指令设置连接总数。

pg_close()不会关闭由pg_pconnect()生成的永久链接。

参数

connection_string

connection_string可以为空使用所有默认参数,也可以包含由空格分隔的一个或多个参数设置。每个参数设置都以表单keyword = value的形式出现。等号周围的空格是可选的。要编写一个空值或包含空格的值,请用单引号括起来,例如keyword ='a value'。值中的单引号和反斜杠必须用反斜杠(即\和\)转义。

目前可识别的参数值是:hosthostaddrportdbnameuserpasswordconnect_timeoutoptionstty(忽略), ,sslmoderequiressl赞成不赞成sslmode),和service。这些参数中的哪一个取决于您的PostgreSQL版本。

connect_type

如果PGSQL_CONNECT_FORCE_NEW通过,则会创建一个新连接,即使这个connection_string连接与现有连接相同。

返回值

PostgreSQL连接资源成功,返回FALSE时失败。

例子

Example #1 Using pg_pconnect()

代码语言:javascript
复制
<?php
$dbconn = pg_pconnect("dbname=mary");
//connect to a database named "mary"

$dbconn2 = pg_pconnect("host=localhost port=5432 dbname=mary");
// connect to a database named "mary" on "localhost" at port "5432"

$dbconn3 = pg_pconnect("host=sheep port=5432 dbname=mary user=lamb password=foo");
//connect to a database named "mary" on the host "sheep" with a username and password

$conn_string = "host=sheep port=5432 dbname=test user=lamb password=bar";
$dbconn4 = pg_pconnect($conn_string);
//connect to a database named "test" on the host "sheep" with a username and password
?>

扫码关注腾讯云开发者

领取腾讯云代金券