pg_fetch_array()错误

时间:2016-05-10 08:59:06

标签: php postgresql syntax-error

我尝试运行此代码以填充包含数据库数据的下拉菜单:

<?php

    if(isset($_GET["table"], $_GET["field"]))
        {
        require "opendb.php";

        $table = $_GET["table"];
        $field = $_GET["field"];

        $query =    "select '{$field}'
                    from '{$table}' "; 

        $data =  pg_query($conn, $query);

        $attribute_names = array();

        while ($row = pg_fetch_array($data))
        {
            array_push($attribute_names, $row[$field]);
        }

        print_r($row);

        echo json_encode($attribute_names);

        require "closedb.php";
    }
?>

但是我得到了错误

  

pg_fetch_array()期望参数1为资源,布尔值为   ...

事情是不知道我是否可以使用$ row liike中的变量$ field。 所以我不知道这是否可能$row[$field]

1 个答案:

答案 0 :(得分:0)

更改

$query =    "select '{$field}'
                    from '{$table}' "; 

$query =    "select `{$field}`
                    from `{$table}` "; 

Table Name&amp; Column Name未表示'。而是使用反引号(`)

在键盘中查找Backtick

enter image description here

修改

由于field/column nametable name在您的查询中不存在而导致错误。试试echo "select {$field} from {$table} ";。并且,看看它出现了什么输出并在 phpmyadmin 中运行此输出,您将得到有关查询的确切错误。

这是正确的$row[$field]