Posts tagged: enum

Enum Type

By สุดเดช, 7 มกราคม 2009 22:07

The ENUM Type

An ENUM is a string object with a value chosen from a list of allowed values that are enumerated explicitly in the column specification at table creation time.

For example, a column specified as ENUM('one', 'two', 'three') can have any of the values shown here. The index of each value is also shown:

enum1

An enumeration can have a maximum of 65,535 elements.

Here is an working example that ensures you that you will get the values inside a enum field in your mysql table.


function enum($table, $field)
{
$result = @mysql_query("show columns from {$table} like \"$field\"");
$result  = @mysql_fetch_assoc($result);
if($result["Type"])
{
preg_match("/(enum\((.*?)\))/", $result["Type"], $enumArray);
$getEnumSet = explode("'", $enumArray["2"]);
$getEnumSet = preg_replace("/,/", "", $getEnumSet);
$enumFields = array();
foreach($getEnumSet as $enumFieldValue)
{
if($enumFieldValue)
{
$enumFields[] = $enumFieldValue;
}
}
return $enumFields;
}
return "Unable to get enum FIELD {$field} from table {$table}";
}

// Returns an array set of all values of a enum field.
print_r(enum("MY_TABLE_NAME","MY_FIELD_NAME"));

link : http://dev.mysql.com/doc/refman/5.0/en/enum.html

Bookmark and Share

Panorama theme by Themocracy