Re: Extracting enum/set values from database
| From: | Al Savage | Date: | Sat, 23 Jun 2001 18:57:02 +0000 |
| Subject: | Re: Extracting enum/set values from database | ||
| Groups: | php.db | ||
| Request: | Send a blank email to php-db+get-9772@lists.php.net to get a copy of this message | ||
** Reply to note from php-db-digest-help@lists.php.net 23 Jun 2001
06:24:11 -0000
Hello, Todd:
> Is there a way to dynamically build an option list from an enum/set
> type? In other words, if I don't know the possible values of the
> enum or set field ahead of time, how can I retrieve that information
> at run-time?
http://www.php.net/manual/en/function.mysql-field-type.php
Look for user comment by
ludwigp@bigfoot.com on 11-Dec-2000 06:57
Where he says:
==================================
I've been asked how to get the try MySQL type of fields, presumably so
folks could get the values of sets and enums.
This is what I use (slightly modified):
Function makeSelectList($table,$field){
$s="";
$rid=mysql_query("SHOW COLUMNS FROM $table");
$nr=mysql_num_rows($rid);
while(list($name, $type)=mysql_fetch_row($rid)){
if($name==$field){
if(ereg('^enum\(.*\)$',$type))
$type=substr($type,6,-2);
else if(ereg('^set\(.*\)$',$type))
$type=substr($type,5,-2);
else
return("<option>ERROR");
$opts=explode("','",$type);
while(list($k,$v)=each($opts))
$s.="<option>$v";
}
}
return($s);
}
Use the function as follows:
echo
"<form><select
name=\"selectfield\">".makeSelectList('tablename',
'field name').
"</select><input type=\"submit\"></form>";
type=\"submit\"></form>";
==================================
Regards,
Al S.
--
* Hillman & other Rootes Group manuals online: http://asavage.fdns.net/Hillman
* Ford Falcon manuals online: http://FalconFAQ.fdns.net
"Bother", said Pooh, and deleted his message base.