mysql field flags_PHP: mysql_field_flags - Manual

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;

}

?>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值