tags:

views:

38

answers:

1

I want to populate a drop down menu with a column from a MySQL database. I have the rest of my database set up, but at the moment it is just a text entry box and it would be easier just to select the actually entry from a list. Also does anyone know of a good tutorial or site that has tutorials and example codes for PHP and MySQL

+1  A: 
$result = // from the database

echo '<select name="foobars">';

while ($row = mysql_fetch_assoc($result))
{
   echo '<option value="'.$row['id'].'">'.$row['name'].'</option>';
}

echo '</select>';
Coronatus
is that it really?
shinjuo
Seems like I should have been able to find that somewhere on google. Most where just a jarbled mess to me
shinjuo
Try it out and see... my fortune-telling ball smashed when I slipped on a banana.
Coronatus
Beat me by 10 seconds :(
Brendan Long
okay I am still new with SQL so what is the $result supposed to be equal to
shinjuo
should add selected="selected" for more complete information
silent
$conn = mysql_query('sql_host', 'sql_username', 'sql_pass');$result = mysql_query('SELECT * FROM blablablabla', $conn);
silent
@selected: that would surely be useful, but they wouldd have to specify the default selected value (or whatever was stored in the session).
geff_chang
@silent okay I understand what you mean now
shinjuo