Tips for Populating Drop-Down Boxes with MySQL Data in PHP
Want to dynamically populate your drop-down boxes with data retrieved from a MySQL database? Here's a quick guide to get you started:
You'll need to start by making sure your connection to the database is established. Specify the hostname, username, password, and database name using mysql_connect() and mysql_select_db().
Next, craft your SQL query. For instance, to fetch PcID values from the PC table, you'd use:
$sql = "SELECT PcID FROM PC";
Using mysql_query($sql), execute the query and store the result in a variable, say $result.
Time to build the drop-down box! Echo a
echo "<select name='PcID'>"; while ($row = mysql_fetch_array($result)) { echo "<option value='" . $row['PcID'] . "'>" . $row['PcID'] . "</option>"; } echo "</select>";
Remember, double-check your connection settings, especially if using a test environment like WAMP where the default username may be 'root'. And voila! Your drop-down box should now seamlessly display the retrieved MySQL data.
The above is the detailed content of How to Populate Drop-Down Boxes with MySQL Data in PHP?. For more information, please follow other related articles on the PHP Chinese website!