简体   繁体   English

PGAdmin 成功但 PDO 查询失败

[英]PGAdmin succeeds but PDO query fails

I am wanting to get column information on a Postgres database table.我想获取有关 Postgres 数据库表的列信息。 I am using the following query:我正在使用以下查询:

select column_name as "Field", data_type as "Type", is_nullable as "Null", character_maximum_length as "max_length" from information_schema.columns where table_name='testempty'

(At some point, I will be removing the AS clauses. I had reasons for including them when I originally set up this query, but these reasons have since evaporated.) (在某些时候,我将删除 AS 子句。当我最初设置此查询时,我有理由将它们包括在内,但这些原因已经不复存在。)

When I run the query in PGAdmin, I get the results I expect: There are 2 columns, and I see their requested details.当我在 PGAdmin 中运行查询时,我得到了我期望的结果:有 2 列,我看到了他们请求的详细信息。 When I execute the same query using PDO in PHP, I get 0 rows back.当我在 PHP 中使用 PDO 执行相同的查询时,我得到了 0 行。 No errors, the execute call returns true.没有错误,执行调用返回 true。 Here is the PHP code:这是 PHP 代码:

try {
    $remote_statement = $remote_con->prepare($column_query);
    $remote_exec = $remote_statement->execute();
} catch(Exception $e) {
    file_put_contents("logs/remote.log", '[' . date("Y-m-d H:i:s") . ' ' . $_SERVER["REMOTE_ADDR"] . "] Prepare failed: " . $e->getMessage() . "\n", FILE_APPEND);
}
if (!$remote_exec) {
    file_put_contents("logs/remote.log", '[' . date("Y-m-d H:i:s") . ' ' . $_SERVER["REMOTE_ADDR"] . "] Execute failed\n", FILE_APPEND);
}
$remote_error = $remote_statement->errorInfo();
if (!empty($remote_error[2])) {
    file_put_contents("logs/remote.log", '[' . date("Y-m-d H:i:s") . ' ' . $_SERVER["REMOTE_ADDR"] . "] Query failed: " . $remote_error[2] . "\n", FILE_APPEND);
    die($remote_error);
}
$remote_rows = $remote_statement->fetchAll(PDO::FETCH_ASSOC);

$remote_con is a PDO connection object I created earlier in the code. $remote_con是我之前在代码中创建的 PDO 连接 object。 $column_query is set to the query I listed above. $column_query设置为我上面列出的查询。 There is another table I run this same code on prior to this and I get the expected results.在此之前我在另一个表上运行相同的代码,我得到了预期的结果。

I appreciate any helpful hints here.我很欣赏这里的任何有用的提示。 I am sure I am missing something obvious, but it baffles me that the query works in PGAdmin and not via a PHP call.我确信我遗漏了一些明显的东西,但让我感到困惑的是,查询在 PGAdmin 中工作,而不是通过 PHP 调用。

This turned out to be a table-specific permissions issue.原来这是一个特定于表的权限问题。 Granted SELECT permissions to PUBLIC for a problem table and the query via PHP worked.授予 SELECT 对 PUBLIC 问题表的权限,并且通过 PHP 的查询有效。 I found another pair of tables in a different database with this issue, and all was resolved by granting this permission.我在另一个数据库中发现了另一对存在此问题的表,并且通过授予此权限来解决所有问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM