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

pg_connect

(PHP 4, PHP 5, PHP 7)

pg_connect - 打开一个PostgreSQL连接

描述

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

pg_connect()打开一个连接到指定的PostgreSQL数据库connection_string

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

具有多个参数$ conn = pg_connect(“host”,“port”,“options”,“tty”,“dbname”)的旧语法 已被弃用。

参数

connection_string

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

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

options参数可用于设置要由服务器调用的命令行参数。

connect_type

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

如果PGSQL_CONNECT_ASYNC给出,则连接是异步建立的。然后可以通过pg_connect_poll()或pg_connection_status()检查连接的状态。

返回值

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

更新日志

描述

5.6.0

支持在添加connect_type时提供PGSQL_CONNECT_ASYNC常量。

例子

Example #1 Using pg_connect()

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

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

$dbconn3?=?pg_connect("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_connect($conn_string);
//connect?to?a?database?named?"test"?on?the?host?"sheep"?with?a?username?and?password

$dbconn5?=?pg_connect("host=localhost?options='--client_encoding=UTF8'");
//connect?to?a?database?on?"localhost"?and?set?the?command?line?parameter?which?tells?the?encoding?is?in?UTF-8
?>

扫码关注腾讯云开发者

领取腾讯云代金券

http://www.vxiaotou.com