如何使用php在MySQL在数据库中获得枚举的可能值?
技术问答
301 人阅读
|
0 人回复
|
2023-09-13
|
我正在设立专栏Mysql:: z! v0 B% T9 D5 v7 @* L
类型: ENUM2 k0 a. S" G" {6 p" ~$ s5 j& x' f
Length/Values:01,02,03,04,05,06,07,08,09' _3 ]7 G" {/ A+ ?
我试图从数据库中检索这些值:
- f. o" W* W2 c* ?2 n1 b/ S我发现了另一个帖子,但也有同样的问题,但我的代码不能正常工作4 Y6 C. Z3 O9 k7 I
$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为ENUM2 b7 J. J1 g/ z# R0 K
: c, H, q# V) S, q4 ~ 解决方案:
2 Q" Y2 J5 m/ `" U! L& p. q0 m 您应该从中分析信息information_schema。columns桌子 -( m2 o {5 s1 i, f5 N" w6 D: U, `
SELECT column_typeFROM information_schema.columnsWHERE table_schema = 'your_schema' AND table_name = 'your_table' AND column_name = 'your_column'…另一个查询-; F% K9 a( v8 @; O7 D: H2 `
SELECT 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分析应用程序中的字符串。 |
|
|
|
|
|