我有一个简单的函数,它查看传入的 mySQL 数据类型,然后将其汇总到“类别”(在此代码中称为系列),以便我可以在类别级别应用默认值。无论如何,这段代码可以很好地查找整数、字符和文本类别,但在日期时间和小数类别上完全失败。我无计可施。任何帮助将不胜感激:
public static function get_family_type ( $col_type ) {
$families = array (
'integer' => array ( 'integer', 'int', 'tinyint', 'mediumint', 'bigint' ),
'fixed' => array ( 'decimal', 'numeric' ),
'floating' => array ( 'float' , 'double' ),
'character' => array ( 'char', 'varchar' ),
'datetime' => array ( 'datetime' , 'timestamp'),
'time' => array ('time'),
'date' => array ('year'),
'text' => array ('tinytext', 'text', 'mediumtext' , 'longtext'),
'blob' => array ('blob','tinyblob','mediumblob','longblob')
);
// first get rid of any optional length parameterisation
list ( $col_type ) = explode ( "(" , $col_type , 2 );
foreach ($families as $family => $family_members) {
if ( array_search ( $col_type , $family_members , true ) ) {
return $family;
}
}
return "unknown $col_type";
}
注意最后一行的备份返回语句,然后查看我在代码中其他地方定义的表的输出。
[18-Jun-2012 17:39:24] Getting default for family of integer
[18-Jun-2012 17:39:24] Getting default for family of text
[18-Jun-2012 17:39:24] Getting default for family of character
[18-Jun-2012 17:39:24] Getting default for family of character
[18-Jun-2012 17:39:24] Getting default for family of character
[18-Jun-2012 17:39:24] Getting default for family of character
[18-Jun-2012 17:39:24] Getting default for family of character
[18-Jun-2012 17:39:24] Getting default for family of character
[18-Jun-2012 17:39:24] Getting default for family of character
[18-Jun-2012 17:39:24] Getting default for family of character
[18-Jun-2012 17:39:24] Getting default for family of character
[18-Jun-2012 17:39:24] Getting default for family of integer
[18-Jun-2012 17:39:24] Getting default for family of integer
[18-Jun-2012 17:39:24] Getting default for family of unknown decimal
[18-Jun-2012 17:39:24] Getting default for family of character
[18-Jun-2012 17:39:24] Getting default for family of unknown datetime
[18-Jun-2012 17:39:24] Getting default for family of floating