Re: Extracting enum/set values from database
| From: | Jason Stechschulte | Date: | Wed, 27 Jun 2001 12:16:05 +0000 |
| Subject: | Re: Extracting enum/set values from database | ||
| References: | 1 | Groups: | php.db |
| Request: | Send a blank email to php-db+get-9860@lists.php.net to get a copy of this message | ||
On Fri, Jun 22, 2001 at 04:45:53PM -0700, Todd A. Jacobs wrote:
> 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?
Here is a function I use to dynamically build an option list. You send
it the table and column. Database and columnType are optional. The
function returns the values of the column as an array.
<?php
function getEnumValues($table, $column, $database="default",
$columnType="enum") {
$mylink = connect();
$sql = "describe $table";
$thisResult = mysql_db_query($database, $sql, $mylink);
while($table = mysql_fetch_array($thisResult)) {
if($table[Field] == $column && $columnType == "enum") {
return explode(",", preg_replace("/^enum\(|'|\)/i", "",
$table[Type]));
}
elseif($table[Field] == $column && $columnType == "set") {
return explode(",", preg_replace("/^set\(|'|\)/i", "",
$table[Type]));
}
}
}
?>
--
Jason Stechschulte
jpstech@unoh.edu
--
It is my job in life to travel all roads, so that some may take the road
less travelled, and others the road more travelled, and all have a
pleasant day.
-- Larry Wall in <199709241628.JAA08908@wall.org>