PHP Conference Japan 2024

pg_close

(PHP 4, PHP 5, PHP 7, PHP 8)

pg_close关闭 PostgreSQL 连接

说明

pg_close(?PgSql\Connection $connection = null): true

pg_close() 关闭与指定 connection 实例关联的 PostgreSQL 数据库的非持久连接。

注意:

通常不需要使用 pg_close(),因为打开的非持久连接会在脚本结束时自动关闭。

如果连接上有打开的 PgSql\Lob 实例,请不要在关闭所有 PgSql\Lob 实例之前关闭连接。

参数

connection

An PgSql\Connection instance. When connection is null, the default connection is used. The default connection is the last connection made by pg_connect() or pg_pconnect().

警告

As of PHP 8.1.0, using the default connection is deprecated.

返回值

总是返回 true

更新日志

版本 说明
8.2.0 现在返回类型为 true;之前是 bool
8.1.0 现在 connection 参数接受 PgSql\Connection 实例,之前接受 resource
8.0.0 connection 现在可以为 null。

示例

示例 #1 pg_close() 示例

<?php
$dbconn
= pg_connect("host=localhost port=5432 dbname=mary")
or die(
"Could not connect");
echo
"Connected successfully";
pg_close($dbconn);
?>

以上示例会输出:

Connected successfully

参见

添加备注

用户贡献的备注 2 notes

up
6
amays
19 years ago
pg_close(...) will not technically close a persistent connection but instead returns it back to the connection pool thus giving you the desired effect of having the connection closed within your script.

http://www.sitepoint.com/article/accessing-postgresql-php/3

best wishes to all.
up
2
mark at redbrick dot dcu dot ie
21 years ago
This function closes the current database connection specified by a handle returned from a pg_connect() call.

<?php
$pgsql_conn
= pg_connect("dbname=mark host=localhost");

if (
$pgsql_conn) {
print
"Successfully connected to: " . pg_host($pgsql_conn) . "<br/>\n";
} else {
print
pg_last_error($pgsql_conn);
exit;
}

// Do database stuff here.

if(!pg_close($pgsql_conn)) {
print
"Failed to close connection to " . pg_host($pgsql_conn) . ": " .
pg_last_error($pgsql_conn) . "<br/>\n";
} else {
print
"Successfully disconnected from database";
}
?>

Of course you normally wouldn't print a message.

Regards, --mark
To Top