ririe44 Posted May 1, 2009 Share Posted May 1, 2009 Hey everyone... I would like to be able to retrieve the unique values of a column of a mysql table to be a list of values in my html <select> code... for example: ColA ---- red blue green red I want it to return... red, blue, green... and make them part of my drop down menu in my form. Can you help me? Quote Link to comment https://forums.phpfreaks.com/topic/156365-retrieve-unique-values/ Share on other sites More sharing options...
mikesta707 Posted May 1, 2009 Share Posted May 1, 2009 You can use the Distinct command in MYSQL something like $query = "SELECT DISTINCTcolumn_name FROM tablename"; Quote Link to comment https://forums.phpfreaks.com/topic/156365-retrieve-unique-values/#findComment-823265 Share on other sites More sharing options...
fry2010 Posted May 1, 2009 Share Posted May 1, 2009 <?php $con = mysql_connect("localhost","name","abc123"); if (!$con) { die('Could not connect: ' . mysql_error()); } mysql_select_db("my_db", $con); $result = mysql_query("SELECT * FROM table"); while($row = mysql_fetch_array($result)) { echo '<option value="'.$row['color'].'">'.$row['colorname'].'</option>'; } mysql_close($con); ?> Quote Link to comment https://forums.phpfreaks.com/topic/156365-retrieve-unique-values/#findComment-823267 Share on other sites More sharing options...
Ken2k7 Posted May 1, 2009 Share Posted May 1, 2009 $query = "SELECT DISTINCT `ColA` FROM `tablename`"; Quote Link to comment https://forums.phpfreaks.com/topic/156365-retrieve-unique-values/#findComment-823269 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.