I'd like to emphasize another way to approach this function.
To check if the return query is null:
Let's say that the table contains a field called 'data'.
if ($ret) {
if (pg_field_is_null($ret, 'data') == 0) {
echo "Nothing returned\n";
<insert code>
}
}