How can I get enum possible values in a MySQL database using PHP?

MySQLMySQLi DatabasePHP

You can get the enum possible values in a MySQL database with the help of INFORMATION_SCHEMA.COLUMNS table. The syntax is as follows −

SELECT
   COLUMN_TYPE AS anyAliasName
FROM
   INFORMATION_SCHEMA.COLUMNS
WHERE
   TABLE_SCHEMA = ‘yourDatabaseName’ AND TABLE_NAME = 'yourTableName' AND COLUMN_NAME = 'yourEnumColumnName';

To understand the above syntax, let us create a table with an ENUM data type. The query to create a table is as follows −

mysql> create table EnumDemo
-> (
-> Id int,
-> Color ENUM('RED','GREEN','BLUE','BLACK','ORANGE')
-> );
Query OK, 0 rows affected (0.66 sec)

Here the table ‘EnumDemo’ is present in the ‘sample’ database. Now you can implement the above syntax to get the all possible enum values from a column.

Example

The query is as follows −

mysql> SELECT
-> COLUMN_TYPE as AllPossibleEnumValues
-> FROM
-> INFORMATION_SCHEMA.COLUMNS
-> WHERE
-> TABLE_SCHEMA = 'sample' AND TABLE_NAME = 'EnumDemo' AND COLUMN_NAME = 'Color';

Output

+---------------------------------------------+
| AllPossibleEnumValues                       |
+---------------------------------------------+
| enum('RED','GREEN','BLUE','BLACK','ORANGE') |
+---------------------------------------------+
1 row in set (0.00 sec)
raja
Published on 19-Mar-2019 07:26:14
Advertisements