phpday 2027 - Call for Papers

pg_close

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

pg_close — Chiude una connessione PostgreSQL

Descrizione

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

pg_close() chiude la connessione non persistente a un database PostgreSQL associata all'istanza connection specificata.

Nota:

Di solito l'uso di pg_close() non è necessario, dal momento che le connessioni aperte non persistenti vengono chiuse automaticamente alla fine dello script.

Se sulla connessione è aperta un'istanza di PgSql\Lob, non chiudere la connessione prima di aver chiuso tutte le istanze di PgSql\Lob.

Elenco dei parametri

connection

Un'istanza di PgSql\Connection. Quando connection è null, viene usata la connessione predefinita. La connessione predefinita è l'ultima connessione effettuata da pg_connect() o pg_pconnect().

Avviso

A partire da PHP 8.1.0, l'uso della connessione predefinita è deprecato.

Valori restituiti

Restituisce sempre true.

Log delle modifiche

Versione Descrizione
8.3.0 Il tipo di ritorno ora è true, in precedenza era bool.
8.1.0 Il parametro connection ora si aspetta un'istanza di PgSql\Connection; in precedenza, era attesa una resource.
8.0.0 connection ora è nullable.

Esempi

Example #1 Esempio di pg_close()

<?php
$dbconn = pg_connect("host=localhost port=5432 dbname=mary")
   or die("Impossibile connettersi");
echo "Connessione riuscita";
pg_close($dbconn);
?>

Il precedente esempio visualizzerà:

Connessione riuscita

Vedere anche:

+add a note

User Contributed Notes 2 notes

up
5
amays
21 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
23 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