我有一个数据库,正在尝试将值提取到列表中
这是我正在尝试运行的代码(我必须从其他地方对其进行调整),它应该返回一个数组,我想从中选择 AllianceName 属性
<?php
//database connection file setting.inc will need to be modified for production
include ("settings.inc");
$con = mysqli_connect("$dbhost", "$dbuser", "$dbpass");
if (!$con) {
exit('Connect Error (' . mysqli_connect_errno() . ') '
. mysqli_connect_error());
}
//set the default client character set
mysqli_set_charset($con, 'utf-8');
mysqli_select_db($dbname, $con);
$options = array();
$options[] = "<option value=''>--?--</option>";
$query = "
SELECT *
FROM `City`
GROUP BY `AllianceName`
";
$db = mysqli_query($query);
foreach ( $db as $d ) {
$options[] = "<option value='{".$d['AllianceName']."}'></option>";
}
?>
<select class="" id="articles" size="1" name="articles">
<?php echo implode("\n", $options); ?>
</select>*/
现在这只返回我在选项数组中定义的 --?-- 。它不解析查询中的任何值。我知道查询本身是正确的,因为我已经在 SQL 服务器中完全按照这种语法运行它并且它可以工作。
我很确定这是一个语法错误... var_dump($db) 给了我一个 bool(false) 输出。
这是我正在使用的最新代码:
<?php
//database connection file setting.inc will need to be modified for production
include ("settings.inc");
$db = new mysqli($dbhost, $dbuser, $dbpass, $dbname);
if($db->connect_errno > 0){
die('Unable to connect to database [' . $db->connect_error . ']');
}
?>
<select class="Select" id="articles" size="1" name="articles">
<?php
$sql = <<<SQL
SELECT DISTINCT `AllianceName`
FROM `City`
SQL;
if(!$result = $db->query($sql)){
die('There was an error running the query [' . $db->error . ']');
}
while($row = $result->fetch_assoc()){
echo "<option value="'.{$row['AllianceName']}.'"></option>";
}
?>
</select>
当我只是回显结果时,查询本身运行良好。每次尝试将其放入下拉列表中都会失败。没有值被填充。