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 ;
}
Related posts:

January 21st, 2010 6:45 pm
[...] Pons has a quick post with a code snippet showing how to grab the possible values for an ENUM or SET field on a MySQL [...]
January 21st, 2010 8:02 pm
[...] Pons has a quick post with a code snippet showing how to grab the possible values for an ENUM or SET field on a MySQL [...]
January 22nd, 2010 10:53 pm
You might be wanting to escape those variables… you know, so someone doesn’t try to get the enum of bobby’; DROP TABLE users;
January 25th, 2010 10:02 am
Sure, you have to use those function with trusted data.
April 7th, 2011 7:56 pm
74. Nice read, I just passed this onto a friend who was doing some research on that. And he actually bought me lunch as I found it for him smile So let me rephrase that: Thank you for lunch!
May 8th, 2011 4:48 pm
Appreciating the persistence you’d put into your site PHP to get enum/set values from mysql field – Barattalo plus detailed tips you present… It’s awesome to arrive upon a blog once in a time that’s not identical old rehashed information. Wonderful article… I have saved your blog post and I am including your Feed into Aol address ! By the way whats the latest on Obama amazing media stories?
July 17th, 2011 5:38 pm
Line 9 uses the now deprecated split function. Recommend replacing it with explode.
August 3rd, 2011 9:03 pm
I’m wondering if we can get the updated version using the explode syntax. Also, as I’m just starting server side I’d like to know if the function is placed in the get/post statement or can it be added to something like phpMyAdmin as a standard function which actually filters the get/post queries? Best regards, Heinz Stapff
August 3rd, 2011 9:22 pm
Just read similar function for ENUM fields on phpfreaks.com that uses a regexpression and the split syntax but I’m in the dark as to wheather the $field has to be specified or the function loops through the whole row of fields and outputs the split info for each enum or set field? Also a complete index of rows as the $row = mysql_fetch_array($result); implies? The topic on phpfreaks is http://www.phpfreaks.com/forums/index.php?topic=163384.msg716137#msg716137. They also showed the echo statement for producing the results on select options but I’m still not sure where to put the function, in the .php? file?
August 9th, 2011 3:15 am
Thanks for making life easier!