RE: [PHP-DB] Pulling the Enum Values from MySQL

From: Date: Fri, 26 Jul 2002 08:24:35 +0000
Subject: RE: [PHP-DB] Pulling the Enum Values from MySQL
Groups: php.db 
Request: Send a blank email to php-db+get-20813@lists.php.net to get a copy of this message
Hi Mark: Check out this little function I hacked from somewhere else a wee while ago, should do what you need. Russ //Name: "enum_select()" Automatically generate an HTML select from MySQL ENUM fieldtype //Author: Russ Michell 2001 :: russ@theruss.com //Arg1: A table name: '$table' //Arg2: A name for the <select> menu '$name' function enum_select($table,$name) { $sql = "SHOW COLUMNS FROM $table"; $result = mysql_query($sql); echo "<select name='$name'>\n\t"; echo "<option>Please make a selection -></option>\n\t"; while($myrow = mysql_fetch_row($result)){ $enum_field = substr($myrow[1],0,4); if($enum_field == "enum"){ global $enum_field; $enums = substr($myrow[1],5,-1); $enums = ereg_replace("'","",$enums); $enums = explode(",",$enums); foreach($enums as $val) { echo "<option value='$val'>$val</option>\n\t"; }//----end foreach }//----end if }//----end while echo "\r</select>"; } -----Original Message----- From: Mark Middleton [mailto:mark@whirledweb.com] Sent: Friday, July 26, 2002 5:41 AM To: php-db@lists.php.net Subject: [PHP-DB] Pulling the Enum Values from MySQL Hi there, I've got a Form I'm building, and would like the drop down boxes to contain the values for one of the fields of type "enum" (not from the actual records in the DB, but the definition of the enum field in MySQL) For example: A field called "userlevel" with enum types "admin" and "visitor". I would like to run a query to get the values "admin" and "visitor" (and any other enum value types) to use in a drop down list on a form. Here's what I've found so far (seems pretty bulky): If you run a query of "SHOW COLUMNS FROM tablename" it gives the returned value of "enum('admin', 'visitor')" in the second column (column called "type") A snippet of code we found in PHPMyAdmin gives this little routine: $enum = str_replace('enum(', '', $row_table_def['Type']); $enum = ereg_replace('\\)$', '', $enum); $enum = explode('\',\'', substr($enum, 1, -1)); $enum_cnt = count($enum); Which apparantly takes the result from the "SHOW COLUMNS..." query and strips out the values. Is this the easiest way to do this using PHP and MySQL? Any help is greatly appreciated - thank you! Mark Middleton Whirled Web -- PHP Database Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php

« previous php.db (#20813) next »