0

This is a awkward issue, and I'm running out of alternatives to workaround.

I have a server running Wamp 3.0.6, with PHP 5.6. My code must connect in a MySQL (local) and a PostgreSQL (remote server).

Everything is working fine, and seemingly out of nowhere it stops returning data from PG. After few seconds or minutes, it just works again.

Even when I'm not able to get data from PG, phpPgAdmin keeps working.

Here is my connection function:

function pdo_pgsql($sql){

    $host = '000.000.000.000';
    $user = 'user';
    $pass = 'pass';
    $db   = 'db';

    try {
        $PDO = new PDO( 'pgsql:host=' . $host . ';dbname=' . $db . ';port=5432', $user, $pass, array(
            PDO::ATTR_PERSISTENT => true
        ));
    }
    catch ( PDOException $e ) {
        echo 'Error: ' . $e->getMessage(); exit;
    }

    $result = $PDO->query( $sql );

    if (is_array($result)){
        $row = $result->fetchAll( PDO::FETCH_ASSOC );
    }else{
        $row = $result;
    }

    return $row;

}

Any suggestion to help me with this?

Thanks

Marcos Felipe
  • 100
  • 13
  • I don't understand this: *My code must connect in a MySQL (local) and a PostgreSQL (remote server)*. Also, no error raised after: *seemingly out of nowhere it stops returning data from PG*? – Parfait Dec 26 '18 at 22:07
  • My code is working fine, but sometimes (apparently without pattern) it stops connecting PG. PDO doesn't return any error or warn, just the same query that was working before, stops working. – Marcos Felipe Dec 26 '18 at 22:15
  • 3
    What **exactly** do you mean by _"stops working"_? I suggest you add `PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION` to your connection options array and [make sure you can see any errors reported](https://stackoverflow.com/questions/845021/how-to-get-useful-error-messages-in-php) – Phil Dec 26 '18 at 23:22
  • It doesn't return any data at all. It's like there was no data to show. I'll include PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION in my function and check if it returns something usefull. Thanks! – Marcos Felipe Dec 27 '18 at 10:14
  • Does the same error occur with `PDO::ATTR_PERSISTENT => false`? And do you really need a persistent connection? – simon.ro Dec 28 '18 at 10:21

1 Answers1

0

I have the same problem, and I found the solution, it is the PDO::ATTR_PERSISTENT need to be set to false.

This fixed my problem.