我想将所有列名提取到下拉菜单中,但下拉菜单保持为空。但我直接在数据库上运行了我使用的查询,效果很好。这是我的代码:
我的索引脚本:
<div class="custom_search col-md-5">
<span>
<select class="form-control" name="column_names" id="column_names">
<?php
while ($row = pg_fetch_array($sql_column_select)) {
echo '<option value="">'. $row['column_name'] .'</option>';
}
?>
</select>
</span>
</div>
我的php脚本:
<?php
$sql_column_select = ("SELECT column_name FROM information_schema.columns WHERE table_name='fastfood_groningen'");
?>
更新的php脚本:
$host = '';
$port = '';
$dbname = '';
$user = '';
$password = '';
//Make connection with database
$conn = pg_connect("host=$host port=$port dbname=$dbname user=$user password=$password");
if (!$conn) {
echo "Not connected : " . pg_error();
exit;
}
$sql_column_select = pg_query($conn, "SELECT column_name FROM information_schema.columns WHERE table_name='fastfood_groningen'");
if (!$sql_column_select) {
echo "An error occurred.n";
exit;
}
我的下拉菜单中仍然没有输出。。。
您的PHP语句需要一个While括号,并且不需要$行周围的单引号外的双引号。
更正代码:
while ($row = pg_fetch_array($sql_column_select)) {
echo '<option value="">'. $row['column_name'] .'</option>';
}
编辑
您实际上并不是在查询数据库。
$sql_column_select = ("SELECT column_name FROM information_schema.columns WHERE table_name='fastfood_groningen'");
需要类似于:
$sql_column_select = pg_exec($ConnectionString, "SELECT column_name FROM information_schema.columns WHERE table_name='fastfood_groningen'");
其中$ConnectionString类似于:
$ConnectionString = "host=sheep port=5432 dbname=test user=lamb password=bar";
请参阅PHP手册