I have a code similar to:
$conn = pg_connect($string_connection );
while(true ){
check if $conn is alive
$result= pg_query ($conn,"SELECT * FROM foo");
do something
sleep(60);
}
How can I check if $conn is alive?
int pg_connection_status($conn) seems like a good indicator.
bool pg_connection_busy($conn) may be interesting as well.
@cobra_fast has answered the question literally, so that answer should be marked correct. However...
You should generally not check if the connection is valid. Except in the case of connection pooler idle checking it's almost always wrong to do so.
Simply use the connection. If something goes wrong, handle the resulting error/exception by closing the connection, opening a new one, and retrying at the beginning of the transaction.
Attempts to "test" the connection first are a waste of time and effort because the connection can break between when you test it and use it, or while you are actively using it. This means you have to handle those error cases anyway... and if you're going to handle them, there's no point bothering to test the connection first.