EdwardJ Posted November 26, 2008 Share Posted November 26, 2008 Hi, I'm trying to fill a select box with mysql results from a query and have come up with the following code: <select name="userlevel"> <?php $query = "SELECT userlevel FROM user_levels ORDER BY userlevel_id"; $result = mysql_query($query); while ($row = mysql_fetch_array($result)){ echo "<option value= .$row .> </option>"; } echo "</select>"; ?> So far the select box shows empty, and I do have at least one field in the table. Also if there is a way to do this with oop, please let me know. Thanks for your help. Link to comment https://forums.phpfreaks.com/topic/134418-solved-fill-select-box-with-query/ Share on other sites More sharing options...
blueman378 Posted November 26, 2008 Share Posted November 26, 2008 <select name="userlevel"> <?php $query = "SELECT * FROM user_levels ORDER BY userlevel_id"; $result = mysql_query($query); while ($row = mysql_fetch_assoc($result)){ echo "<option value=\"$row[userlevel]\"> </option>"; } echo "</select>"; ?> try that Link to comment https://forums.phpfreaks.com/topic/134418-solved-fill-select-box-with-query/#findComment-699780 Share on other sites More sharing options...
Maq Posted November 26, 2008 Share Posted November 26, 2008 echo ""; Link to comment https://forums.phpfreaks.com/topic/134418-solved-fill-select-box-with-query/#findComment-699782 Share on other sites More sharing options...
Maq Posted November 26, 2008 Share Posted November 26, 2008 $query = "SELECT userlevel FROM user_levels ORDER BY userlevel_id"; For an optimized query you should really only SELECT the fields you need like above, not *. Link to comment https://forums.phpfreaks.com/topic/134418-solved-fill-select-box-with-query/#findComment-699785 Share on other sites More sharing options...
DeanWhitehouse Posted November 26, 2008 Share Posted November 26, 2008 Do you mean it is empty with no drop down options or are there drop down options just empty ones. anyway here is a code fix <select name="userlevel"> <?php $query = "SELECT userlevel FROM user_levels ORDER BY userlevel_id DESC"; $result = mysql_query($query); while ($row = mysql_fetch_array($result)) { echo "<option value= \"".$row['userlevel']."\">".$row['userlevel']."</option>"; } echo "</select>"; ?> For an optimized query you should really only SELECT the fields you need like above, not *. @Maq, depends on the size of the table and the amount of rows it has. Link to comment https://forums.phpfreaks.com/topic/134418-solved-fill-select-box-with-query/#findComment-699789 Share on other sites More sharing options...
Maq Posted November 26, 2008 Share Posted November 26, 2008 echo "".$row['userlevel'].""; Here's something more readable. Link to comment https://forums.phpfreaks.com/topic/134418-solved-fill-select-box-with-query/#findComment-699794 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.