snorky Posted June 25, 2009 Share Posted June 25, 2009 How can I create an array from the result set of a mysql select statement? mysql code: select distinct bldg from main order by bldg; Quote Link to comment Share on other sites More sharing options...
Nightwolf629 Posted June 25, 2009 Share Posted June 25, 2009 loop through the results, and add each row to an array $query = "select distinct bldg from main order by bldg"; $result = mysql_query($query); $my_array = array(); while ($row = mysql_fetch_assoc($result)){ $my_array[] = $row['bldg']; } Quote Link to comment Share on other sites More sharing options...
Richard Posted June 25, 2009 Share Posted June 25, 2009 How can I create an array from the result set of a mysql select statement? mysql code: select distinct bldg from main order by bldg; Have a look at mysql_fetch_array(), mysql_fetch_assoc() and mysql_fetch_row() Quote Link to comment Share on other sites More sharing options...
snorky Posted June 25, 2009 Author Share Posted June 25, 2009 THX !! I had to change one line to make it work: $my_array[] = $row['bldg']; became $my_array = $row['bldg']; Without that change the code didn't work. What I did with your solution is to create a 'dynamic' menu using HTML <select><option>... The menu offers all possible values for the bldg field, even if values are added or lost - and the list is always ordered in whatever way the query calls: $db=mysql_select_db("roster",$link); $query = "select distinct bldg from main where bldg !='' order by bldg"; $result = mysql_query($query); $my_array = array(); $line=0; // initialize a counter for while loop echo "<select type='text' name='titleb' value='' />"; while ($row = mysql_fetch_assoc($result)) { $line++; $my_array = $row['bldg']; echo "<option value =" . "'$my_array'" . ">" . $my_array . "</option>"; } echo "</select>"; No doubt there are more elegant ways to create that menu, but the concept is simple and I'm already thinking about other ways to use it. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.