Retrieve valid entries for a MySQL ENUM/SET column
November 23rd, 2005
Here's a handy little PHP function I threw together to retrieve valid entries for a MySQL ENUM/SET column, handy for generating dynamic radio group/checkbox/multiple select HTML elements:
PHP:
-
// grab possible SET/ENUM values and return an array
-
function getPossibleValues($table,$field){
-
-
$query = "SHOW COLUMNS FROM `$table` LIKE '$field'";
-
-
return $options;
-
} else {
-
return false;
-
}
-
-
}
Add to del.icio.us
Digg this
Technorati
Related Posts:
4 Comments Add your own
1. dan | January 11th, 2006 at 1:56 pm
Although it doesn't this doesn't affect the correctness of the script, there should be a semicolon after "return $options"
2. Harvey A. Ramer | June 24th, 2006 at 1:46 pm
Thanks for this code snippet. It does exactly what I want!
I do have one small problem though. On my system the function returns a string like this for the opening array value:
set('value
And for the closing value it returns:
value')
Do you have a quick suggestion for how to make the preg_replace function work? I'm on PHP 4.
3. Domenico | August 18th, 2006 at 10:39 am
This work for me:
preg_replace("/(?:enum|set)('(.+?)')/","\1", $fields)
4. Passerby | March 5th, 2008 at 3:46 pm
None of the solutions suggest prior to this post have worked for me, so I came up with this:
preg_replace("/')$/", '', preg_replace("/^(enum|set)('/", '', $fields))
Note: I'm using PHP 5.2.0 with mysqli functions
This function is a great idea though. Thanks for sharing.
Leave a Comment
Some HTML allowed:
<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <code> <em> <i> <strike> <strong>
Trackback this post | Subscribe to the comments via RSS Feed