Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

HELP!! Enum field in mysql

Status
Not open for further replies.

rbauerle

Programmer
Oct 16, 2001
48
BR
is there a way i can get the values of enum fields indexes?
I wonder if there's a function as there is mysql_field_name() to get the name of fields.

If anyone could help, thanx in advance.
 
I don't know about a specific function, but here's one way to do it:

Given the following table:
[tt]+---------+--------------------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+---------+--------------------------+------+-----+---------+-------+
| id | int(10) unsigned | | PRI | 0 | |
| enumval | enum('Yes','No','Maybe') | | | No | |
+---------+--------------------------+------+-----+---------+-------+
[/tt]

The following code will get you the list of enum values for the column enumval in an array:

[tt]
Code:
<?php

$link = mysql_connect (&quot;localhost&quot;, &quot;test&quot;, &quot;test&quot;);

mysql_select_db (&quot;test&quot;, $link);

$query = &quot;show columns from foo like 'enumval'&quot;;
$result = mysql_query ($query, $link);
$vals = mysql_fetch_object ($result);

$enums = preg_split (&quot;/,/&quot;, preg_replace (&quot;/enum|\)|\(/&quot;, &quot;&quot;, $vals->Type));

print_r ($enums);

?>
[/tt]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top