Welcome Guest, Not a member yet? Register   Sign In
Is it possible to get ENUM values from database fields
#1

[eluser]mabright[/eluser]
I have a table in my MySQL database and there are alot of attribute fields. I normally create a foreign key table to hold the possible field values but there were just so many fields that I decided to make them all ENUM and define the allowable values on the field.

Is there a way I can read the values of the ENUM so I can print them as values in my forms drop down boxes?
#2

[eluser]JanDoToDo[/eluser]
I believe you can use the INFORMATION SCHEMA, i.e.

SELECT COLUMN_TYPE
FROM INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_SCHEMA = 'YourDatabase'
AND TABLE_NAME = 'YourTable'
AND COLUMN_NAME = 'YourEnum'

COLUMN_TYPE
-----------------
enum('A','B','C','D')
#3

[eluser]mabright[/eluser]
That did the trick, thanks.
#4

[eluser]nuwanda[/eluser]
[quote author="JanDoToDo" date="1264403700"]I believe you can use the INFORMATION SCHEMA, i.e.

SELECT COLUMN_TYPE
FROM INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_SCHEMA = 'YourDatabase'
AND TABLE_NAME = 'YourTable'
AND COLUMN_NAME = 'YourEnum'

COLUMN_TYPE
-----------------
enum('A','B','C','D')[/quote]

Thanks, but exactly how do I extract the enum as discrete values?

I'm doing this...

Code:
$qstring="SELECT COLUMN_TYPE FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_SCHEMA = 'bst_ci_db' AND TABLE_NAME = 'users' AND COLUMN_NAME = 'user_role'";
        $query = $this->db->query($qstring);
        echo '<pre>';
        print_r($query->row());
        echo '</pre>';
        }

and getting this:

Code:
stdClass Object
(
    [COLUMN_TYPE] => enum('member','admin')
)

Man, I feel thick.




Theme © iAndrew 2016 - Forum software by © MyBB