如何使用php在MySQL在数据库中获得枚举的可能值?
技术问答
310 人阅读
|
0 人回复
|
2023-09-13
|
我正在设立专栏Mysql:
' P- S$ X" F" V1 N5 E8 _3 \类型: ENUM
1 t5 H/ m# R! N1 @3 p$ j, F! y; PLength/Values:01,02,03,04,05,06,07,08,09
4 F! S [6 R/ H" ^ S1 B; z6 g我试图从数据库中检索这些值:
# r, c8 r& s# f* A- ?* s& l; u% J我发现了另一个帖子,但也有同样的问题,但我的代码不能正常工作7 Y7 g% C% w, x5 q9 y
$type = $mysqli->query( "SHOW COLUMNS FROM {$tableName} WHERE Field = 'type'" )->fetch_object()->Type; preg_match('/^enum\((.*)\)$/',$type,$matches); foreach( explode(',',$matches[1]) as $value $enum[] = trim( $value,"'" );;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;return $enum;我输入的类型Text为ENUM. E$ W, z. W- }# c$ n) c5 K: m
7 u5 c) x V0 M4 k$ e 解决方案: 5 b" E+ q/ J5 ^) \! E
您应该从中分析信息information_schema。columns桌子 -
& E- k+ l: G: R/ X/ V FSELECT column_typeFROM information_schema.columnsWHERE table_schema = 'your_schema' AND table_name = 'your_table' AND column_name = 'your_column'…另一个查询-
3 n* m( Z# G1 F& r7 hSELECT TRIM(TRAILING ')' FROM TRIM(LEADING '(' FROM TRIM(LEADING 'enum' FROM column_type))) column_typeFROM information_schema.columnsWHERE table_schema = 'your_schema' AND table_name = 'your_table' AND column_name = 'your_column';会有这样的事- enum01,02,03。php分析应用程序中的字符串。 |
|
|
|
|
|