473,387 Members | 1,463 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

reading enum field from database

pradeepjain
563 512MB
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 ???
Oct 6 '09 #1
8 4036
Atli
5,058 Expert 4TB
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.
Oct 6 '09 #2
Atli
5,058 Expert 4TB
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.
Oct 6 '09 #3
pradeepjain
563 512MB
http://www.imranulhoque.com/mysql/bu...r-enum-fields/

i found this post with a function to create dropdown ... this helped me !! :)
Oct 6 '09 #4
pradeepjain
563 512MB
that link worked for me but i am getting the values like 'cdma' 'gsm' ....i dont what's the reason :(
Oct 6 '09 #5
Atli
5,058 Expert 4TB
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.
Oct 7 '09 #6
pradeepjain
563 512MB
@Atli
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>';
Oct 8 '09 #7
Atli
5,058 Expert 4TB
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)
Oct 8 '09 #8
pradeepjain
563 512MB
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 .
Oct 13 '09 #9

Sign in to post your reply or Sign up for a free account.

Similar topics

3
by: Pjotr Wedersteers | last post by:
Hi I am not even sure if it is at all possible, and I can't find the stuff I am looking for, probably using the wrong search keys. WAMP machine (XP, 2.50.2, 4.1.5, 5.0.2) I have a field in...
7
by: Charles Crume | last post by:
Hello all; I have used dBASE, and other computer languages/databases, for years. They all have a logical field type. However, the version of MySQL used by the ISP hosting my site does not...
8
by: Bruno BAGUETTE | last post by:
Hello, I have to migrate a MySQL database to a PostgreSQL database without procedures. The problem is that this MySQL database uses ENUM, do you see what can I do to migrate ENUM into...
9
by: Lawrence Oluyede | last post by:
I have a list of strings and i'd like to build up an enum from them... is there a way to do that? Thanks in advance. -- Lawrence "Rhymes" Oluyede http://loluyede.blogspot.com
1
by: Alex | last post by:
I have a simple enough struct.. public struct InstallationGeneral_Lock { public int Case_name; public int Run_year; public int Inst_name; public int Inst_loc; public int State; public int...
2
by: filipk69 | last post by:
Could someone point me in the right direction, please. I have an enum something like this: enum TextFieldType { FieldABC = 3000, FieldXYZ = 3001, ….. ….. }
4
by: =?Utf-8?B?0JrQvtC70LXQstCw?= | last post by:
I want to use an enum field in a constructor. For example: class Student { private string name; private enum university {.....}; private byte course; ......
2
by: Daniel Jeffrey | last post by:
Hello. Can anyone help me please. I have created an Enumeration, and I loop through it, it all works ok, except the first item returns 2 times. Code is below. When called I Get "Text Edit" 2...
11
by: =?Utf-8?B?dG9iaXdhbl9rZW5vYmk=?= | last post by:
The following code is in a custom deserializer: object value = (int) 1; string nameToParse = Enum.GetName(field.FieldType, value); value = Enum.Parse(field.FieldType, nameToParse); Currently...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.