Justtechjobs.com Find a programming school near you






Online Campus Both


php-db | 2001062

[PHP-DB] Re: Extracting enum/set values from database From: Al Savage (asavage <email protected>)
Date: 06/23/01

** Reply to note from php-db-digest-help <email protected> 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 <email protected> 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.

-- PHP Database Mailing List (http://www.php.net/) To unsubscribe, e-mail: php-db-unsubscribe <email protected> For additional commands, e-mail: php-db-help <email protected> To contact the list administrators, e-mail: php-list-admin <email protected>