如果您願意使用許多函數來獲取表格中欄位的一些資訊,那麼這個函數適合您。如果您只想取得所有可以找到的資訊,您可以使用這個函數。
<?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;
}
?>