Jump to content

Getting ENUM field possible values from database


flaab

Recommended Posts

This can be done using the PHP's describe table feature. It will list the enum field, the next problem is using Regular Expressions with this. Since I'm not the perfect person for helping with Regular Expressions, I checked out php.net and found a function that does just this (with an example for populating dropdown box)

function mysql_enum_values($tableName,$fieldName)
{
  $result = mysql_query("DESCRIBE $tableName");

  //then loop:
  while($row = mysql_fetch_array($result))
  {
    //# row is mysql type, in format "int(11) unsigned zerofill"
    //# or "enum('cheese','salmon')" etc.

    ereg('^([^ (]+)(\((.+)\))?([ ](.+))?$',$row['Type'],$fieldTypeSplit);
    //# split type up into array
    $ret_fieldName = $row['Field'];
    $fieldType = $fieldTypeSplit[1];// eg 'int' for integer.
    $fieldFlags = $fieldTypeSplit[5]; // eg 'binary' or 'unsigned zerofill'.
    $fieldLen = $fieldTypeSplit[3]; // eg 11, or 'cheese','salmon' for enum.

    if (($fieldType=='enum' || $fieldType=='set') && ($ret_fieldName==$fieldName) )
    {
      $fieldOptions = split("','",substr($fieldLen,1,-1));
      return $fieldOptions;
    }
  }

  //if the funciton makes it this far, then it either
  //did not find an enum/set field type, or it
  //failed to find the the fieldname, so exit FALSE!
  return FALSE;

}

EXAMPLE

echo "<SELECT NAME=\"Select\" SIZE='1'>";
foreach($fieldOptions as $tmp)
{
  echo "<OPTION>$tmp";
}

Hope this helps  :-D

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.