【问题标题】:Running Posgres query with PDO: Invalid argument supplied for foreach()使用 PDO 运行 Postgres 查询:为 foreach() 提供的参数无效
【发布时间】:2017-03-14 18:05:07
【问题描述】:

试图效仿http://php.net/manual/en/pdo.query.php中的例子 和连接

$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() 提供的参数无效

这里有什么问题?

【问题讨论】:

  • @JayBlanchard 不,你不知道,它甚至写在手册中。至于这种情况 - 显然 query() 返回 false 而不是 pdo 语句。
  • 哎呀@u_mulder - 这似乎是个坏主意。

标签: php postgresql pdo


【解决方案1】:

As manual says:

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

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

【讨论】:

    猜你喜欢
    • 2012-12-05
    • 2012-10-10
    • 2013-04-29
    • 2017-04-25
    • 1970-01-01
    • 1970-01-01
    • 2011-05-17
    • 2011-02-07
    相关资源
    最近更新 更多