How do I get a column from mySQL to show up in a select box via PHP? [closed]
Questions asking us to recommend or find a tool, library or favorite off-site resource are off-topic for Stack Overflow as they tend to attract opinionated answers and spam. Instead, describe the problem and what has been done so far to solve it.
Closed 9 years ago.
Improve this question 开发者_开发百科I have a form with one select box (pulldown list). I need to get a column from mySQL to appear in the select box. How do i make the connection? Are there any examples out there?
<?php
// connect to MySQL
$query = "SELECT <col> FROM <table>";
$result = mysql_query($query);
?>
<select name="sel" id="sel">
<?php
while($row = mysql_fetch_assoc($result)){
echo "<option>{$row['<col>']}</option>";
}
?>
</select>
If you need to have the first option empty, just print this line after the select
tag:
<option></option>
I've not clearly understood, what ever you want to say, but you can try the below solution.
<select name="example">
<?php
// Database connetcion goes here
$q=mysql_query("SELECT option1 FROM table"); /// where option1=the column you want to show
while ($sql=mysql_fetch_row($q)){
echo "<option value='".$q['0']."'>".$q['0']."</option>";
}
/// keep this code block wher you want to show the pull down select box
?>
</select>
精彩评论