Connecting Tech Pros Worldwide Forums | Help | Site Map

reading enum field from database

pradeepjain's Avatar
Needs Regular Fix
 
Join Date: Jul 2007
Location: India
Posts: 411
#1: Oct 6 '09
hii guys ,
I have a mysql enum column field with say
Expand|Select|Wrap|Line Numbers
  1. 'type'   enum(cdma,gsm,both) 

is there a way from php to read the enums and make it a dropdown list ???

Atli's Avatar
Moderator
 
Join Date: Nov 2006
Location: Iceland
Posts: 3,758
#2: Oct 6 '09

re: reading enum field from database


Hi.

You can get the type of the column using the "SHOW COLUMNS" command:
Expand|Select|Wrap|Line Numbers
  1. mysql> SHOW COLUMNS FROM `table`LIKE 'type';
  2. +-------+---------------------------+------+-----+---------+-------+
  3. | Field | Type                      | Null | Key | Default | Extra |
  4. +-------+---------------------------+------+-----+---------+-------+
  5. | type  | enum('cdma','gsm','both') | YES  |     | NULL    |       |
  6. +-------+---------------------------+------+-----+---------+-------+
And from there you could just parse the values out of the "Type" field.
Atli's Avatar
Moderator
 
Join Date: Nov 2006
Location: Iceland
Posts: 3,758
#3: Oct 6 '09

re: reading enum field from database


On second thought, you would probably be better of using the PHP functions specifically made to get that info:
mysql_list_fields or mysql_field_type.

That should get you the type, which you can extract the values from.
pradeepjain's Avatar
Needs Regular Fix
 
Join Date: Jul 2007
Location: India
Posts: 411
#4: Oct 6 '09

re: reading enum field from database


http://www.imranulhoque.com/mysql/bu...r-enum-fields/

i found this post with a function to create dropdown ... this helped me !! :)
pradeepjain's Avatar
Needs Regular Fix
 
Join Date: Jul 2007
Location: India
Posts: 411
#5: Oct 6 '09

re: reading enum field from database


that link worked for me but i am getting the values like 'cdma' 'gsm' ....i dont what's the reason :(
Atli's Avatar
Moderator
 
Join Date: Nov 2006
Location: Iceland
Posts: 3,758
#6: Oct 7 '09

re: reading enum field from database


Isn't that what you wanted? To get the enum values like that?

Anyways, we can't do much unless you show us the code, and perhaps an example of the output you are getting, and the output you actually wanted.
pradeepjain's Avatar
Needs Regular Fix
 
Join Date: Jul 2007
Location: India
Posts: 411
#7: Oct 8 '09

re: reading enum field from database


Quote:

Originally Posted by Atli View Post

Isn't that what you wanted? To get the enum values like that?

Anyways, we can't do much unless you show us the code, and perhaps an example of the output you are getting, and the output you actually wanted.

yeah i wanted the same but output is getting displayed along with the quotes like 'gsm' instead of gsm !!!




Expand|Select|Wrap|Line Numbers
  1. function getEnumFieldValues($tableName, $fieldName){
  2. $field_query = mysql_query("show columns from $tableName where Field='$fieldName'");
  3. if(mysql_num_rows($field_query) <= 0) return false;
  4. $fieldDetail = mysql_fetch_array($field_query);
  5. $type = preg_replace('/(^set\()|(^enum\()/i', '', $fieldDetail['Type']);
  6. $enumFields = substr($type, 0, -1);
  7. $fieldSplit = split(',', $enumFields);
  8. return $fieldSplit;
  9. }
  10.  
  11.  
  12.  
  13. $enumFields = getEnumFieldValues('agree', 'test');
  14. echo '<select name="agree">';
  15. foreach($enumFields as $value){
  16. echo '<option value="' . $value . '">' . $value . '</option>';
  17. }
  18. echo '</select>';
Atli's Avatar
Moderator
 
Join Date: Nov 2006
Location: Iceland
Posts: 3,758
#8: Oct 8 '09

re: reading enum field from database


Ahh so it's just the quote-marks that need to be removed?

Try using the str_replace function on the "$enumFields" string *before* it is split into induvidual words. (Which happens on line #7)
pradeepjain's Avatar
Needs Regular Fix
 
Join Date: Jul 2007
Location: India
Posts: 411
#9: Oct 13 '09

re: reading enum field from database


i dono how strange it is but the same thing i use for value and displaying

Expand|Select|Wrap|Line Numbers
  1.  echo '<option value=' . $value . ">' . $value . '</option>';
the value which gets stored in DB gets stored with out quotes . while displaying alone quotes getting displayed .
Reply