littlevisuals Posted July 14, 2009 Share Posted July 14, 2009 Hi all How do I create an option dropdown using data from mysql? I have 2 fields (artist_firstname, artist_lastname) I need merging together in this box (as well as ordering alphabetically). I've tried this code and it doesn't work, but then again im not sure if im even selecting the tables correctly My database ARTISTS tables in ARTISTS are "ARTISTS, CATORGORIES & GALLERY" here is the code <label for="artist">Artists</label><SELECT NAME="artist"> <?php mysql_connect("localhost","root","password"); mysql_select_db("artists"); $q = "SELECT artist_firstname, artist_lastname FROM `artists` ORDER BY name ASC"; $r = mysql_query($q); while($column = mysql_fetch_assoc($r)){ $artist_lastname = $column['id']; $artist_lastname = $column['artist_lastname']; echo '<OPTION VALUE="'.$artist_firstname.'">'.$artist_lastname.'</option>'; } ?> </SELECT> How would I insert the data (artist_firstname + artist_lastname) into one field? Sorry if its something stupid i've just started learning php/mysql! Quote Link to comment Share on other sites More sharing options...
AwptiK Posted July 14, 2009 Share Posted July 14, 2009 echo '<OPTION VALUE="'.$artist_firstname.'.$artist_lastname.'">'.$artist_firstname.' '.$artist_lastname.'</option>'; Ex: <option value="BonJovi">Bon Jovi</option> Quote Link to comment Share on other sites More sharing options...
Psycho Posted July 14, 2009 Share Posted July 14, 2009 <?php mysql_connect("localhost","root","password"); mysql_select_db("artists"); $query = "SELECT id, CONCAT(artist_firstname, ' ', artist_lastname) as name FROM `artists` ORDER BY artist_lastname ASC"; $result = mysql_query($query); $artist_options = ''; while($row = mysql_fetch_assoc($r)) { $artist_options .= "<option value=\"{$row['id']}\">{$row['name']}</option>\n"; } ?> <label for="artist">Artists</label> <SELECT NAME="artist"> <?php echo $artist_options; ?> </SELECT> Quote Link to comment Share on other sites More sharing options...
littlevisuals Posted July 14, 2009 Author Share Posted July 14, 2009 Hi thanks everyone for some solutions with your code mjdamato i get Warning: mysql_fetch_assoc(): supplied argument is not a valid MySQL result resource in Sorry adding $result to the fetch did it Thankyou! Quote Link to comment Share on other sites More sharing options...
Psycho Posted July 14, 2009 Share Posted July 14, 2009 Sorry adding $result to the fetch did it yeah, sorry, it is a pet peeve of mine when people use variable names that offer absolutely no value to identifying what the variable is. It may seem more efficient when initially writing the code, but it creates problems when having to edit the code later or if someone else needs to edit the code. So, I will sometimes go a little too far when editing peoples code on forums. 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.