从数据库中提取值以填充下拉列表

时间:2013-04-09 21:31:22

标签: php mysqli html-select

我有一个数据库,我正在尝试将值提取到列表中

这是我试图运行的代码(我必须从其他地方调整它),它应该返回一个数组,我想从中选择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>*/

现在这只返回我在options数组中定义的 - ? - 。它不解析查询中的任何值。我知道查询本身是正确的,因为我已经在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>

当我回显结果时,查询本身运行正常。每次尝试将其放入下拉列表都会失败。没有值填充。

3 个答案:

答案 0 :(得分:1)

这里有几个问题。 首先,您不需要查询中的GROUP BY 'AllianceName',您没有对数据执行任何功能,这可能导致您不返回任何结果。

其次,通常使用while循环遍历查询结果。您不必这样做,但通常的做法是,所以您的代码应该是这样的..

<?php
//database connection file setting.inc will need to be modified for production
include ("settings.inc");  

$con = mysqli_connect("$dbhost", "$dbuser", "$dbpass", "$dbname");
if (!$con) {
  exit('Connect Error (' . mysqli_connect_errno() . ') '
       . mysqli_connect_error());
} 
//there is no need to make an array first, just have it spit out the options if you aren't making a class or function
?>
<select class="" id="articles" size="1" name="articles">
<?php
$query = "SELECT * FROM `City` ";
$db = mysqli_query($query);
while ( $d=mysqli_fetch_assoc($db)) {
  echo "<option value='{".$d['AllianceName']."}'></option>";
}
?>
</select>

尝试一下,看看它是否适合你。

答案 1 :(得分:0)

来自mysqli_query()文档:

  

失败时返回FALSE。成功的SELECT,SHOW,DESCRIBE或   EXPLAIN查询mysqli_query()将返回一个mysqli_result对象。对于   其他成功的查询mysqli_query()将返回TRUE。

所以查询成功后你拥有的是mysqli_result对象。现在,您必须逐行将结果提取到(关联)数组或对象中。

if($query === false) die('Query failed to execute');

while($row = $result->fetch_assoc()) {
    echo $row['AllianceName'] . PHP_EOL;
}

所有这些都在网络上的数百个教程中有所描述,所以请阅读其中一个。我推荐这些在当前十年写的,例如http://codular.com/php-mysqli

答案 2 :(得分:-1)

这是最终有效的代码。 @Syndrose和@Zombiehunter都提供了线索。

@Syndrose - 您可能需要考虑此处使用的语法,因为这是您的代码库失败的原因...尤其是echo标记行的语法。

<?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 . ']');
} 
$sql = <<<SQL
    SELECT DISTINCT `AllianceName`
    FROM `City`
SQL;
if(!$result = $db->query($sql)){
    die('There was an error running the query [' . $db->error . ']');
    }
?>
<select  style="width:300px" class="" id="AllianceName" size="1" name="Alliance Name">
<?php
while($row = $result->fetch_assoc()){
     echo '<option value='.$row['AllianceName'].'>'.$row['AllianceName'].'</option>';
}
?>
</select>