如何使用php在MySQL数据库中获取枚举可能的值? [英] How can I get enum possible values in a MySQL database using php?

查看:258
本文介绍了如何使用php在MySQL数据库中获取枚举可能的值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

可能重复:
MySQL选择枚举值

Possible Duplicate:
Mysql Select Enum Values

我在Mysql中设置了一个提示框:

I have set up a coloumn in Mysql:

类型:ENUM

Length/Values: '01','02','03','04','05','06','07','08','09','10','11','12'

我正在尝试从数据库中检索这些值:

I'm trying to retrive those values from the DB:

我发现了另一个问题相同的帖子,但是我的代码不起作用

I found another post with the same question but my code didn't work

$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

推荐答案

您应该从information_schema.columns表-

SELECT
  column_type
FROM
  information_schema.columns
WHERE
  table_schema = 'your_schema' AND table_name = 'your_table' AND column_name = 'your_column'

...另一个查询-

SELECT
  TRIM(TRAILING ')' FROM TRIM(LEADING '(' FROM TRIM(LEADING 'enum' FROM column_type))) column_type
FROM
  information_schema.columns
WHERE
  table_schema = 'your_schema' AND table_name = 'your_table' AND column_name = 'your_column';

会有这样的东西-enum('01','02','03').在php应用程序中解析此字符串.

There will be something like this - enum('01','02','03'). Parse this string in the php-application.

这篇关于如何使用php在MySQL数据库中获取枚举可能的值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆