PHP to get enum/set values from mysql field
This function returns an array with the elements allowed in a ENUM or SET mysql field. This can be usefull…
This function returns an array with the elements allowed in a ENUM or SET mysql field. This can be usefull if you’re making some automation and need to retrieve those values without writing them in your code:
function set_and_enum_values( $table , $field ){
$query = "SHOW COLUMNS FROM `$table` LIKE '$field'";
$result = mysql_query( $query ) or die( 'Error getting Enum/Set field ' . mysql_error() );
$row = mysql_fetch_array($result);
if(stripos(".".$row[1],"enum(") > 0) $row[1]=str_replace("enum('","",$row[1]);
else $row[1]=str_replace("set('","",$row[1]);
$row[1]=str_replace("','","\n",$row[1]);
$row[1]=str_replace("')","",$row[1]);
$ar = split("\n",$row[1]);
for ($i=0;$i<count($ar);$i++) $arOut[str_replace("''","'",$ar[$i])]=str_replace("''","'",$ar[$i]);
return $arOut ;
}