日期:2014-05-16  浏览次数:20743 次

如何获取mysql 字段类型?
用DLL包

while((field = mysql_fetch_field(m_Result))) //获取该表字段名称保存在CStrligList中
{
  str = field->name; //能获取字段名称
  str1 = field->type; //获取的字段类型是乱码????? 


}

5.5.21 已经不支持mysql_field_type()获取字段函数了。。

如何获取mysql 字段类型?


------解决方案--------------------
SELECT * FROM `information_schema`.`COLUMNS` where `TABLE_NAME`='tbname' and `TABLE_SCHEMA`='dbname'
------解决方案--------------------
field->type 是个枚举。


enum enum_field_types type

The type of the field. The type value may be one of the MYSQL_TYPE_ symbols shown in the following table.

Type Value Type Description
MYSQL_TYPE_TINY TINYINT field
MYSQL_TYPE_SHORT SMALLINT field
MYSQL_TYPE_LONG INTEGER field
MYSQL_TYPE_INT24 MEDIUMINT field
MYSQL_TYPE_LONGLONG BIGINT field
MYSQL_TYPE_DECIMAL DECIMAL or NUMERIC field
MYSQL_TYPE_NEWDECIMAL Precision math DECIMAL or NUMERIC
MYSQL_TYPE_FLOAT FLOAT field
MYSQL_TYPE_DOUBLE DOUBLE or REAL field
MYSQL_TYPE_BIT BIT field
MYSQL_TYPE_TIMESTAMP TIMESTAMP field
MYSQL_TYPE_DATE DATE field
MYSQL_TYPE_TIME TIME field
MYSQL_TYPE_DATETIME DATETIME field
MYSQL_TYPE_YEAR YEAR field
MYSQL_TYPE_STRING CHAR or BINARY field
MYSQL_TYPE_VAR_STRING VARCHAR or VARBINARY field
MYSQL_TYPE_BLOB BLOB or TEXT field (use max_length to determine the maximum length)
MYSQL_TYPE_SET SET field
MYSQL_TYPE_ENUM ENUM field
MYSQL_TYPE_GEOMETRY Spatial field
MYSQL_TYPE_NULL NULL-type field

You can use the IS_NUM() macro to test whether a field has a numeric type. Pass the type value to IS_NUM() and it evaluates to TRUE if the field is numeric:

if (IS_NUM(field->type))
printf("Field is numeric\n");

------解决方案--------------------
$sql1 = "show full columns from $tb_name";
$quy1 = mysql_query($sql1);
while($val1 = mysql_fetch_array($quy1))
{
$type = $val1[type];
}