PHP Unconference Europe 2015

pg_connection_status

(PHP 4 >= 4.2.0, PHP 5)

pg_connection_status Zwraca stan połączenia

Opis

int pg_connection_status ( resource $połączenie )

pg_connection_status() zwraca status podanego połączenia.

Parametry

połączenie

Zasób, połączenie do bazy PostgreSQL.

Zwracane wartości

PGSQL_CONNECTION_OK lub PGSQL_CONNECTION_BAD.

Przykłady

Przykład #1 pg_connection_status() przykład

<?php
  $dbconn 
pg_connect("dbname=publisher") or die("Nie można się połączyć");
  
$stat pg_connection_status($dbconn);
  if (
$stat === PGSQL_CONNECTION_OK) {
      echo 
'Status połączenia ok';
  } else {
      echo 
'Status połączenia bad';
  }    
?>

Zobacz też:

add a note add a note

User Contributed Notes 3 notes

up
0
Mathieu De Zutter
8 years ago
Being careful with the comparison operators is not enough:

<?php
if (pg_connection_status($link)===PGSQL_CONNECTION_BAD)
  
reconnect($link);
?>

The reconnect won't be trigged when $link is null.

The manual is just wrong, there are three return values: PGSQL_CONNECTION_OK, PGSQL_CONNECTION_BAD, null
up
0
david dot tulloh at infaze dot com dot au
9 years ago
I think zytox is incorrect, at least in PHP 5.0.4.
It returns null, but you have to be careful with your comparison operators.

As an example:
<?php
unset($null);
if (
pg_connection_status($null)===PGSQL_CONNECTION_OK)
    echo
'this is not called';
if (
pg_connection_status($null)==PGSQL_CONNECTION_OK)
    echo
'this is called because NULL==0 is true';
?>
up
0
zytox at hotmail dot com
9 years ago
If the connection variable is NULL this function returns 0 in PHP 5.0.2. Havent figured out any more erratic values for the connection variable but be careful.
To Top