ok, sorry for the code bloat :) but this is how I
get the full power of mysql's DESCRIBE table statement, in
an associative array, including defaults, enum values, float radix et all.
it assumes mysql returns the type as
"type[(arg[,arg..])] [ add]"
like
"float(20,6) unsigned"
"enum('yes','no')"
etc
<?
function getFields($tablename) {
$fields = array();
$fullmatch = "/^([^(]+)(\([^)]+\))?(\s(.+))?$/";
$charlistmatch = "/,?'([^']*)'/";
$numlistmatch = "/,?(\d+)/";
$fieldsquery .= "DESCRIBE $tablename";
$result_fieldsquery = mysql_query($fieldsquery) or die(mysql_error());
while ($row_fieldsquery = mysql_fetch_assoc($result_fieldsquery)) {
$name = $row_fieldsquery['Field'];
$fields[$name] = array();
$fields[$name]["type"] = "";
$fields[$name]["args"] = array();
$fields[$name]["add"] = "";
$fields[$name]["null"] = $row_fieldsquery['Null'];
$fields[$name]["key"] = $row_fieldsquery['Key'];
$fields[$name]["default"] = $row_fieldsquery['Default'];
$fields[$name]["extra"] = $row_fieldsquery['Extra'];
$fulltype = $row_fieldsquery['Type'];
$typeregs = array();
if (preg_match($fullmatch, $fulltype, $typeregs)) {
$fields[$name]["type"] = $typeregs[1];
if ($typeregs[4]) $fields[$name]["add"] = $typeregs[4];
$fullargs = $typeregs[2];
$argsreg = array();
if (preg_match_all($charlistmatch, $fullargs, $argsreg)) {
$fields[$name]["args"] = $argsreg[1];
} else {
$argsreg = array();
if (preg_match_all($numlistmatch, $fullargs, $argsreg)) {
$fields[$name]["args"] = $argsreg[1];
} else die("cant parse type args: $fullargs");
}
} else die("cant parse type: $fulltype");
}
return $fields;
}
?>