Here's an example of how to get the ENUM into a drop down list in a web
form.  I use it all the time..

[code]
/*
Get_Enum : Retreives the "enum" options from an "enum" type field in a
MySql Database 
*/
 
function Get_enum($table,$field,$db,$link) {

mysql_select_db($db,$link);
$result = mysql_query("show columns from $table like '$field'", $link);
$query_data = mysql_fetch_array($result);

if(eregi("('.*')", $query_data["Type"], $match)) {
        $enum_str = ereg_replace("'", "", $match[1]);
        $enum_options = explode(',', $enum_str);
}

array_push($enum_options, $query_data["Default"]);
return $enum_options;
}



//Note: db_connect() (not shown) is a function created to make a
database connection...


<form>
<select size="1" name="category" style="border-style: solid;
border-width: 1; padding: 1">
<?php
                $link_id = db_connect(dbhost,dbname,dbuser,dbpass);
                $array =
Get_enum("tablename","Fieldname",$dbname,$link_id);
                foreach($array as $var) echo "<option
value='$var'>$var</option>";
?>
            </select>
</form>
[code]

Hope this helps...

Jeff

> -----Original Message-----
> From: Alan Lord [mailto:[EMAIL PROTECTED] 
> Sent: Wednesday, November 19, 2003 12:12 PM
> To: [EMAIL PROTECTED]
> Subject: [PHP] retrieving ENUM description from MySQL
> 
> 
> Hi all,
> 
> This is a bit wierd so don't flame please.
> 
> Imagine a database table, field defined as ENUM with a list 
> of allowed types such as "Mr", "Mrs", "Miss", "Dr", "Prof", 
> etc - you get the idea.
> 
> I've read the MySQL manual and by using
> 
> SHOW COLUMNS FROM "table_name" LIKE "enum_column_name";
> 
> I can get a result.
> 
> But the string of ENUMs is not an array. It is returned as a 
> string with all types in one string.
> 
> Anyone got any ideas about how to retrieve it in a better way 
> - else I'll need to start exploding the string...
> 
> Thanks in advance
> 
> Al
> 
> -- 
> PHP General Mailing List (http://www.php.net/)
> To unsubscribe, visit: http://www.php.net/unsub.php
> 
> 

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

Reply via email to