I want to populate an HTML <select>
with options from an ENUM field in a MySQL database using PHP and PHP Data Objects (PDO). How can I do this?
Vanilla PHP implementation:
<select>
<?
$result = mysql_query('SHOW COLUMNS FROM '.$table_name.' WHERE field="'.$column_name.'"');
while ($row = mysql_fetch_row($result)) {
foreach(explode("','",substr($row[1],6,-2)) as $option) {
print("<option>$option</option>");
}
}
?>
<select>
PHP Data Objects implementation
<select>
<?
$sql = 'SHOW COLUMNS FROM '.$table_name.' WHERE field="'.$column_name.'"';
$row = $db->query($sql)->fetch(PDO::FETCH_ASSOC);
foreach(explode("','",substr($row['Type'],6,-2)) as $option) {
print("<option>$option</option>");
}
?>
</select>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With