将MySQL表链接到下拉列表

时间:2016-04-18 09:44:04

标签: php mysql database phpmyadmin dropdown

我正在尝试将我的数据库表链接到下拉列表,因此下拉列表中填充了我的表中的值。有什么可以帮助我或有任何建议吗? 这是我的整个dogdropdown.php

    <?php

 // set up connection parameters
 $dbHost        = 'localhost';
$databaseName   = 'i insert my db name here';
$username       = 'i insert my username here';
$password       = 'i insert my password here';

// make the database connection
$db = new PDO("mysql:host=$dbHost;dbname=$databaseName;charset=utf8", "$username", "$password");
$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);   // enable error handling
$db->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);           // turn off emulation mode

 $mysqli=mysqli_connect($dbHost,$username,$password,$databaseName);
?>



  <h3> Search dogs</h3>
<select>
<?php
$stmt = $mysqli->query("SELECT * FROM dog");// wrap off and use only query to run
$stmt->execute();
$stmt->store_result();

/* Get the number of rows */
$num_of_rows = $stmt->num_rows;

/* Get the result */
$result = $stmt->get_result();

while ($dropdown = $result->fetch_assoc()) {
print_r($dropdown);
// echo '<option value="Dog_Id"' . $dropdown['id'] . '">'.$dropdown['id'].'</option>';// add text to dropdown
 }
 ?>
  </select>

我希望下拉菜单中填充表狗和Dog_Id列的值。 数据库连接似乎没问题,因为没有显示错误!

1 个答案:

答案 0 :(得分:0)

面临的问题是$dropdown['dog']尝试将其替换为$ dropdown ['id']。因为,我认为在 DOG表

中有没有名称为狗的列
<h3> Search dogs</h3>
<select>
<?php
$stmt = $mysqli->query("SELECT * FROM dog");// wrap off and use only query to run
$stmt->execute();
$stmt->store_result();

/* Get the number of rows */
$num_of_rows = $stmt->num_rows;

/* Get the result */
$result = $stmt->get_result();

 while ($dropdown = $result->fetch_assoc()) {
    print_r($dropdown);
   // echo '<option value="Dog_Id"' . $dropdown['id'] . '">'.$dropdown['id'].'</option>';// add text to dropdown
}
?>
</select>