Posted By

johnself on 02/27/07


Tagged

mysql php enum


Versions (?)

Who likes this?

3 people have marked this snippet as a favorite

oso96_2000
vali29
canberkol


Get Enumerated Values for a MySQL Field


 / Published in: PHP
 

Returns an array of values specified for an enumerated MySQL field

  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  

You need to login to post a comment.