- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- MS Excel
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How can I get enum possible values in a MySQL database using PHP?
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)
Advertisements