Jump to content

[SOLVED] $query = 'SELECT col1, col2 FROM table - how do I access the columns?


greencoin

Recommended Posts

Was just foolin around with some code I have and thought it would be cool to query a database to populate a dropdown box but the option value is not the same as the option content. So I changed my query to individually grab the columns I needed with this;

 

$query = 'SELECT mcode, manu FROM gcmanu';

 

but now I'm stuck cause I don't know how to grab the individual variables, col1, col2.  I could make it work using two queries but I understand that's not the preferred way as it double database traffic.

 

Any advice is appreciated. Thanks ~Rich

<?php

$query = 'SELECT mcode, manu FROM gcmanu';
$result = mysql_query($query);

echo '<select name="name">';

while ($row = mysql_fetch_assoc($result)){
   echo '<option value="'.$row['mcode'].'">'.$row['gcmanu'].'</option>';
}

echo '</select>';

?>

 

That will give you a populated drop down box where the value will be whatever is in "mcode" and the displayed option will be whatever is in "gcmanu".

 

I wasn't really clear on how you exactly wanted it...so let me know if that is what you wanted.

well, I modified my code using what you gave me and I get a drop down box that drops a long ways but has nothing in it. I'm using godaddy hosting with mysql 4.1.

 

** corrected - I created a product using the "blank" dropdown and on my query the field was filled in with a value so I'm thinkin there's something to the quotes where the option text would go....the value part is fine.

 

** corrected, corrected: your quotes were fine, I was using the wrong field name..  :-X Thanks again ~Rich

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.