Jump to content

[SOLVED] Finding enum options of a column in a mysql database


redbrad0

Recommended Posts

I know I can run SHOW CREATE TABLE TableName but it just outputs the following info

 

CREATE TABLE `TableName` ( `Table_ID` int(11) unsigned NOT NULL auto_increment, `Table_Type` enum('Promoter','Media','Corporate') NOT NULL default 'Promoter', PRIMARY KEY (`Table_ID`) ) ENGINE=MyISAM DEFAULT CHARSET=latin1

 

How can I just return the enum values in red?

 

 

Link to comment
Share on other sites

Thank you for the reply. For reference for anyone else that might find this thread, here is what I am using.

 

function getEnumOptions($table, $field) {
   $finalResult = array();

   if (strlen(trim($table)) < 1) return false;
   $query  = "show columns from $table";
   $result = mysql_query($query);
   while ($row = mysql_fetch_array($result)){
        if ($field != $row["Field"]) continue;
        //check if enum type
        if (ereg('enum.(.*).', $row['Type'], $match)) {
            $opts = explode(',', $match[1]);
            foreach ($opts as $item)
                $finalResult[] = substr($item, 1, strlen($item)-2);
        }
        else
                return false;
   }
   return $finalResult;
}

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.