List:General Discussion« Previous MessageNext Message »
From:Paul DuBois Date:May 24 2003 3:29am
Subject:Re: ENUM
View as plain text  
At 22:35 -0400 5/23/03, Urb LeJeune wrote:
>>>radio buttons. I detect the field is an ENUM using mysql_field_flags(). What
>>>I want to do next if determine the "M', 'S', 'H', 'D' enumeration.
>>
>>Do you mean you want to determine the relationship between the string
>>enumeration values and their numeric equivalents?
>
>         No, I just want to determine there are four enumerated values
>which are M,S,H, D.
>
>>Why not just make the list element values and labels both the same, i.e.,
>>the string values?
>
>         I have an aversion to making life easy for the programmer at the
>expense of the user :-)
>
>>Otherwise, you need to get the column definition (e.g., with SHOW
>>COLUMNS), yank out the list of values, and iterate through them to
>>determine their numeric values.  What language are you using?  C?
>>PHP?
>
>         PHP.
>
>         I could dig into PHPmyAdmin to see how they do it.

Yeah.  Or you can may be able to use this:

# get_enumorset_info() - get metadata for an ENUM or SET column.
# Take a connection identifier, a table name, and a column name.
# Return an associative array with name, type, values, default,
# and nullable elements.  Return FALSE if no info available or
# an error occurs.

function get_enumorset_info ($conn_id, $tbl_name, $col_name)
{
     # escape any SQL pattern characters in column name
     $col_name = ereg_replace ("([%_])", "\\\\1", $col_name);
     $query = "SHOW COLUMNS FROM $tbl_name LIKE '$col_name'";
     if (!($result_id = mysql_query ($query, $conn_id)))
         return (FALSE);
     if (!($row = mysql_fetch_row ($result_id)))
         return (FALSE);
     mysql_free_result ($result_id);
     $info = array ("name" => $row[0]);
     if (!preg_match ("/^(enum|set)\((.*)\)$/", $row[1], $match))
         return (FALSE);
     $info["type"] = $match[1];
     # split value list at commas
     $info["values"] = explode (",", $match[2]);
     # remove surrounding quotes from values
     $info["values"] = preg_replace ("/^'(.*)'$/", "\\1", $info["values"]);
     # determine whether or not column can contain NULL values
     $info["nullable"] = ($row[2] == "YES");
     # get default value (unset value represents NULL)
     $info["default"] = $row[4];
     return ($info);
}

It's part of the recipes distribution that accompanies MySQL Cookbook,
http://www.kitebird.com/mysql-cookbook/

>
>         Thanks again for your time and help.
>
>Urb


-- 
Paul DuBois
http://www.kitebird.com/
sql, query
Thread
ENUMUrb LeJeune24 May
  • Re: ENUMMartin Gainty24 May
  • Re: ENUMPaul DuBois24 May
    • Re: ENUMUrb LeJeune24 May
      • Re: ENUMPaul DuBois24 May
        • Re: ENUMUrb LeJeune24 May
          • Re: ENUMYerra Venkata Srinivasa Rao24 May
          • Re: ENUMPaul DuBois24 May