Just created this function for retrieving the allowed values for an ENUM type field in a MySQL DB. Tested out OK on my web host (PHP 4 / MySQL 4.0). Just curious if anyone sees any obvious stupidities before I add it to by MySQL class. (Also thought someone else might have a use for it some day.)
/**
* array getEnumValues(str $table, str $column)
* Purpose: get an array of the allowed values for a MySQL ENUM column
* Note: assumes already connected to MySQL and DB has been selected
*/
function getEnumValues($table, $column)
{
$sql = "SHOW COLUMNS FROM `$table` LIKE '$column'";
$result = mysql_query($sql);
if(!$result)
{
user_error(mysql_error()."<br>$sql", E_USER_WARNING);
return(FALSE);
}
if(!mysql_num_rows($result))
{
user_error("No info found for specified column", E_USER_WARNING);
return(FALSE);
}
$row = mysql_fetch_assoc($result);
if(!preg_match('/^enum/i', $row['Type']))
{
user_error("Not an ENUM type field", E_USER_WARNING);
return(FALSE);
}
$string = preg_replace('/^enum/i', 'array', $row['Type']);
eval("\$enumValues = $string;");
if(!is_array($enumValues))
{
user_error("Unknown error in getEnumValues()", E_USER_WARNING);
return(FALSE);
}
return($enumValues);
}