基于MySQL列数据类型的动态PHP表单创建

基于MySQL列数据类型的动态PHP表单创建,php,mysql,sql,Php,Mysql,Sql,如何知道使用select从数据库中提取的每列的类型,以便在表单中动态添加正确的输入类型?有几种方法可以做到这一点;两者都不完美,但两者都会起作用 首先,如果知道表和列名,可以执行以下操作: SELECT data_type, character_maximum_length, numeric_precision FROM information_schema.COLUMNS WHERE TABLE_SCHEMA = DATABASE() AND TABLE_NAME='table'

如何知道使用select从数据库中提取的每列的类型,以便在表单中动态添加正确的输入类型?

有几种方法可以做到这一点;两者都不完美,但两者都会起作用

首先,如果知道表和列名,可以执行以下操作:

SELECT data_type, character_maximum_length, numeric_precision 
  FROM information_schema.COLUMNS
 WHERE TABLE_SCHEMA = DATABASE()
   AND TABLE_NAME='table'
   AND COLUMN_NAME='column'
鉴于当前数据库中的表和列名,这将为您提供构造字段所需的一些基本信息。数据类型的值包括以下内容:

bigint     64-bit integer
char       fixed length character string
datetime   date and time
decimal    decimal
double     64-bit IEEE-754 floating point
enum       enumerated type (small integer)
float      32-bit IEEE-754 floating point
geometry   Geo extension type
int        32-bit integer
longblob   binary large object, up to 4 gigabytes in size
longtext   text large object, up to 4 gigabytes in size
mediumblob binary large object, up  16 megabytes in size
mediumint  integer in range [0-16 megabytes]
mediumtext text large object, up to 16 megabytes
smallint   integer [0-65535]
text       text large object, up to 64k bytes
time       time of day
timestamp  UNIX style timestamp
tinyint    integer [0-255]
tinyblob   binary "large" object up to 255 bytes
tinytext   text "large" object up to 255 bytes
varchar    variable length character string

其次,当您使用SELECT语句从RDMS MySQL或其他数据库获取结果集时,您总是会返回一些描述列的信息(一些元数据)。您可以使用mysqli::fetch_字段或其他API中的等效调用来检索该字段。在本例中,您将返回编码值,如下所述:

您可以从适当命名的信息\u模式表中获取模式信息: