Jump to content

MYSQL PHP Dropdown


Aimless

Recommended Posts

Hi,

 

I'm trying to create a dropdown box that is filled by a mysql column. I found this example, which fills the dropdown box with a list of columns in the database, but I want to fill the drop down box with the content in a column.

Any guidance on how to do this would be appreciated.

 

Thank you!

 

Example:

$connection = mysql_connect("localhost","username","password"); 
$fields = mysql_list_fields("database", "table", $connection); 
$columns = mysql_num_fields($fields); 
echo "<form action=page_to_post_to.php method=POST><select name=Field>"; 
for ($i = 0; $i < $columns; $i++) { 
echo "<option value=$i>"; 
echo mysql_field_name($fields, $i); 
} 
echo "</select></form>"; 

Link to comment
https://forums.phpfreaks.com/topic/209225-mysql-php-dropdown/
Share on other sites

You will need to use a while loop from a query to get all the contents from the column.

 

$query = "SELECT value FROM database";
$result = mysql_query($result);

echo "<select>";

while ($rows = mysql_fetch_array($result) {

echo "<option value='$rows['value']'>$rows['value']</option>";
}

echo "</select>"

Link to comment
https://forums.phpfreaks.com/topic/209225-mysql-php-dropdown/#findComment-1092563
Share on other sites

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.