Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

PHP MySQL populate drop down list

Status
Not open for further replies.

kpdvx

Programmer
Dec 1, 2001
87
US
Anyone have any experience in this area?

-Thanks.
 
<?php

$result = mysql_query(&quot;SELECT drop_down FROM database&quot;,$db);

echo &quot;<select name=drop_down>\n&quot;;
while ($row = mysql_fetch_array($result)) {
echo &quot;<option value=$row[0]>$row[0]</option>\n&quot;;
}
echo &quot;</select>\n&quot;;




?> ***************************************
Party on, dudes!
[cannon]
 
OK all notes are //<notes> the // will preven php from parsing this text.

----------------dropdown.php----------------------
<?php // begin php code

// connect to mySQL
$db = mysql_connect(&quot;localhost&quot;, &quot;username&quot;, &quot;password&quot;) or die ('Cannot connect to database');

// select the database
mysql_select_db(&quot;your_database&quot;,$db) or die ('database not available');

// select the column from your database which contains the items to populate the drop down list
$sql='SELECT drop_down FROM database';

// run the query on the selected database using the connection details above
$result = mysql_query($sql,$db);

// start the drop-down html
echo &quot;<select name=drop_down>\n&quot;;

// split the query results into rows to populate the select box
while ($row = mysql_fetch_array($result)) {

// add each item in the result to the select box
echo &quot;<option value=$row[0]>$row[0]</option>\n&quot;;

// end the while statement
}

// end the select box
echo &quot;</select>\n&quot;;

// close the php tag
?> ***************************************
Party on, dudes!
[cannon]
 
My bad..
$sql='SELECT drop_down FROM database';
should read:
$sql='SELECT drop_down FROM your_table'; ***************************************
Party on, dudes!
[cannon]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top