0
votes

Trying to follow the example in http://php.net/manual/en/pdo.query.php and connecting with

$dbh = new PDO('pgsql:host=localhost;port=5432;dbname=example;user=postgres');

and running query

foreach ($dbh->query('SELECT * from Animals') as $row) {
  print $row;
}

I am getting the error:

Warning: Invalid argument supplied for foreach() in /Users/zaitsev/tests/pg-php/pdo-test.php on line 13

What is wrong here?

1
@JayBlanchard No, you don't, it's even written in a manual. As for this case - obviously query() returns false instead of pdo statement. - u_mulder
Yikes @u_mulder - that just seems a bad idea. - Jay Blanchard

1 Answers

1
votes

As manual says:

PDO::query() returns a PDOStatement object, or FALSE on failure.

It's very probable that your query is returning false by some failure. Ensure your PostgreSQL connection and try out execute your query directly on PostgreSQL (PGAdmin).