如果您愿意使用很多函数来获取有关表中字段的少量信息,那么此函数适合您。如果您只想获取所有可找到的信息,可以使用以下内容
<?php
function getPrimaryKeyOf($table) {
$keys = Array();
$query = sprintf("SHOW KEYS FROM `%s`", $table);
$result = mysql_query($query) or die(mysql_error());
while ($row = mysql_fetch_assoc($result)) {
if ( $row['Key_name'] == 'PRIMARY' )
$keys[$row['Seq_in_index'] - 1] = $row['Column_name'];
}
return $keys;
}
function getTableInformationOf($table) {
$information = array(
"auto" => "",
"primary" => array(),
"fields" => array()
);
$information['primary'] = $this->getPrimaryKeyOf($table);
$result = mysql_query("DESC `$table`");
while ( $field = mysql_fetch_assoc($result) ) {
$information['fields'][] = $field;
if ( $field['Extra'] == "auto_increment" )
$information['auto'] = $field['Field'];
}
return $information;
}
?>