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
2010-03-23 04:49:46
is that it really?
shinjuo
2010-03-23 04:50:37
Seems like I should have been able to find that somewhere on google. Most where just a jarbled mess to me
shinjuo
2010-03-23 04:51:16
Try it out and see... my fortune-telling ball smashed when I slipped on a banana.
Coronatus
2010-03-23 04:51:35
Beat me by 10 seconds :(
Brendan Long
2010-03-23 04:51:41
okay I am still new with SQL so what is the $result supposed to be equal to
shinjuo
2010-03-23 04:54:43
should add selected="selected" for more complete information
silent
2010-03-23 04:55:40
$conn = mysql_query('sql_host', 'sql_username', 'sql_pass');$result = mysql_query('SELECT * FROM blablablabla', $conn);
silent
2010-03-23 04:57:31
@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
2010-03-23 04:59:30
@silent okay I understand what you mean now
shinjuo
2010-03-23 05:01:24