从MySQL列数据填充下拉列表 [英] Populate drop down list from MySQL column data

查看:245
本文介绍了从MySQL列数据填充下拉列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前无法在下拉列表的数据库列中显示条目.

I am currently having trouble displaying the entries in a column of my database in a drop down list.

下面的代码...我想是可行的... 当我运行它时,下拉列表为我提供6个选项(数据库中的列也有6列)现在,我只需要显示名称即可!

The code below... is half working i guess... When I run it, the drop down list gives me 6 options (the column in my database also has 6 columns) Now I just need to get the names to show up!

感谢所有帮助!

<select id="teamlist" name="teamlist">
<?php
  $pdo = new PDO('mysql:host=localhost;dbname=clubresults', 'root', '12345678');
  #Set Error Mode to ERRMODE_EXCEPTION.
  $pdo->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);  
  $stmt = $pdo->prepare('Select teamname from members');
  $stmt->execute();
  while($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
    echo "<option>{$row['teamname']}</option>";
  }
?>
</select> 

推荐答案

 <select id="teamlist" name="teamlist">
 <?php
        $pdo = new PDO('mysql:host=localhost;dbname=clubresults', 'root', '12345678');
        #Set Error Mode to ERRMODE_EXCEPTION.
        $pdo->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);  

    $stmt = $pdo->prepare('Select teamname from members');
    $stmt->execute();
       while($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
    echo '<option>'.$row['teamname'].'</option>';
       }
     ?>
 </select> 

这篇关于从MySQL列数据填充下拉列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆