Get Enumerated Values for a MySQL Field


/ Published in: PHP
Save to your folder(s)

Returns an array of values specified for an enumerated MySQL field


Copy this code and paste it in your HTML
  1. function enum($table, $field) {
  2. $result = @mysql_query("show columns from {$table} like \"$field\"");
  3. $result = @mysql_fetch_assoc($result);
  4. if($result["Type"])
  5. {
  6. preg_match("/(enum\((.*?)\))/", $result["Type"], $enumArray);
  7. $getEnumSet = explode("'", $enumArray["2"]);
  8. $getEnumSet = preg_replace("/,/", "", $getEnumSet);
  9. $enumFields = array();
  10. foreach($getEnumSet as $enumFieldValue)
  11. {
  12. if($enumFieldValue)
  13. {
  14. $enumFields[] = $enumFieldValue;
  15. }
  16. }
  17. return $enumFields;
  18. }
  19. return false;
  20. }

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.