...

View Full Version : Display MySQL Data In A Form



Troy297
10-26-2006, 09:30 PM
Hi All,

I am having some trouble with using the MySQL "SELECT" statement to retrieve one COLUMN of data and then placing it into a select form input....

My current syntax is below but I am not sure how I would insert the variables into the form once I select them from the database.... ps. a loop statement would be nice to repeatedly select until there is nothing left...


<?php
$result = mysql_query("SELECT username FROM users");
echo "<option>";
echo "";
echo "</option>";
?>

Any help is greatly appreciated!

Brandoe85
10-26-2006, 09:47 PM
Hi,

Loop the results with mysql_fetch_assoc();


<?php
$result = mysql_query("SELECT username FROM users");
echo '<select name="users">';
while($row = mysql_fetch_assoc($result))
{
echo '<option value="' . $row['username'] . '">' . $row['username'] . '</option>';
}
echo '</select>';
?>
Ref:
http://us2.php.net/control_structures
http://us2.php.net/mysql_fetch_assoc

Troy297
10-27-2006, 12:26 PM
wow! thanks - works like a charm... but what would I need to add to have the "select" options show in alphabetical order by username?

thxs

MRMAN
10-27-2006, 01:16 PM
$result = mysql_query("SELECT username FROM users ORDER BY username ASC");



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum