问问题
7046 次
2 回答
8
香草 PHP 实现:
<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 数据对象实现
<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>
于 2012-05-30T22:13:46.217 回答
4
这是另一种解决方案,当您想将枚举值放入数组中以根据需要使用时,您可以将其用作辅助函数。
public static function getSQLEnumArray($table, $field, $db){
$row = $db->query("SHOW COLUMNS FROM ".$table." LIKE ".$field)->fetch(PDO::FETCH_ASSOC);
preg_match_all("/'(.*?)'/", $row['Type'], $categories);
$fields = $categories[1];
return $fields;
}
于 2014-02-25T18:19:37.603 回答