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…

January 19, 2010

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]);
	$ar = split("\n",$row[1]);
	for ($i=0;$i<count($ar);$i++) $arOut[str_replace("''","'",$ar[$i])]=str_replace("''","'",$ar[$i]);
	return $arOut ;


I'm a software engineer, an everyday web developer and a maker. I usually build sites with PHP, within or without WordPress. I build Internet of Things with Arduino and ESP8266. I'm the founder of and and I'm actually the Chief Technical Officer of Better Days web agency.

Comments on “PHP to get enum/set values from mysql field”

10 thoughts

  1. SeanJA says:

    You might be wanting to escape those variables… you know, so someone doesn’t try to get the enum of bobby’; DROP TABLE users;

  2. admin says:

    Sure, you have to use those function with trusted data.

  3. 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!

  4. 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?

  5. Darien says:

    Line 9 uses the now deprecated split function. Recommend replacing it with explode.

  6. Heinz Stapff says:

    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

  7. Heinz Stapff says:

    Just read similar function for ENUM fields on 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 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?

  8. Brian says:

    Thanks for making life easier!

Comments are closed


MYSQL add counter in a query

Use mysql variables to create a counter in SQL, PHP code to use an SQL counter and what does the i mean in mysqli?

November 9, 2019

MySQL fulltext search always empty

If you’ve just switched to FULLTEXT indexes and you’ve just started playing with MATCH... AGAINST syntax, but you always get…

November 23, 2013

How to remove custom fields from WordPress

Ok. You’ve added custom fields in wordpress, but how can you remove them? How to delete custom fields wordpress Two…

September 29, 2013

13 mysql tips that you don’t know

Here is a list of thirteen tips that can be usefull for your queries. If you know more tips send…

October 19, 2012

PHP function to fix collation on database fields of MySQL

This PHP function search for tables in the active db, match table’s name with the regular expression passed as first…

February 11, 2010

10 PHP usefull functions for MySQLi stuff (mysql improved)

I’ve modified the previous post to use mysqli instead of mysql. Was it necessary? I think it was good step…

January 29, 2010