Php 使用PDO运行Posgres查询:为foreach()提供的参数无效

Php 使用PDO运行Posgres查询:为foreach()提供的参数无效,php,postgresql,pdo,Php,Postgresql,Pdo,试着在下面的例子中 与 $dbh = new PDO('pgsql:host=localhost;port=5432;dbname=example;user=postgres'); 并运行查询 foreach ($dbh->query('SELECT * from Animals') as $row) { print $row; } 我得到一个错误: 警告:为第13行/Users/zaitsev/tests/pg php/pdo-test.php中的foreach()提供的参数无效

试着在下面的例子中 与

$dbh = new PDO('pgsql:host=localhost;port=5432;dbname=example;user=postgres');
并运行查询

foreach ($dbh->query('SELECT * from Animals') as $row) {
  print $row;
}
我得到一个错误:

警告:为第13行/Users/zaitsev/tests/pg php/pdo-test.php中的foreach()提供的参数无效

这里怎么了?


您的查询很可能由于某种故障而返回
false
。确保您的PostgreSQL连接,并尝试直接在PostgreSQL(PGAdmin)上执行查询。

@JayBlanchard不,您不需要,它甚至写在手册中。对于这种情况,显然
query()
返回
false
而不是pdo语句.Yikes@u_mulder,这似乎是个坏主意。
PDO::query() returns a PDOStatement object, or FALSE on failure.