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; Link to comment https://forums.phpfreaks.com/topic/163652-solved-create-php-array-from-mysql-select-statement/ 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']; } Link to comment https://forums.phpfreaks.com/topic/163652-solved-create-php-array-from-mysql-select-statement/#findComment-863479 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() Link to comment https://forums.phpfreaks.com/topic/163652-solved-create-php-array-from-mysql-select-statement/#findComment-863482 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. Link to comment https://forums.phpfreaks.com/topic/163652-solved-create-php-array-from-mysql-select-statement/#findComment-863599 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.