pg_fetch_array() 错误
pg_fetch_array() 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() expects parameter 1 to be resource, boolean given in
...
问题是我不知道我是否可以像那样在 $row 中使用变量 $field。
所以我不知道这是否可能 $row[$field]
.
改变
$query = "select '{$field}'
from '{$table}' ";
到
$query = "select `{$field}`
from `{$table}` ";
Table Name
& Column Name
未通过 '
表示。而是使用反引号 (`)
在键盘中查找 Backtick
:
编辑
出现错误,因为 field/column name
或 table name
在您的查询中不存在。尝试 echo "select {$field} from {$table} ";
。并且,在 phpmyadmin 中查看它的输出和 运行 这个输出,您将得到关于查询的确切错误。
没错$row[$field]
我尝试运行此代码用我的数据库中的数据填充下拉菜单:
<?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() expects parameter 1 to be resource, boolean given in ...
问题是我不知道我是否可以像那样在 $row 中使用变量 $field。
所以我不知道这是否可能 $row[$field]
.
改变
$query = "select '{$field}'
from '{$table}' ";
到
$query = "select `{$field}`
from `{$table}` ";
Table Name
& Column Name
未通过 '
表示。而是使用反引号 (`)
在键盘中查找 Backtick
:
编辑
出现错误,因为 field/column name
或 table name
在您的查询中不存在。尝试 echo "select {$field} from {$table} ";
。并且,在 phpmyadmin 中查看它的输出和 运行 这个输出,您将得到关于查询的确切错误。
没错$row[$field]