dynamic dropdown

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
danwguy
Forum Contributor
Posts: 256
Joined: Wed Nov 17, 2010 1:09 pm
Location: San Diego, CA

dynamic dropdown

Post by danwguy »

I am trying to create a dropdown list populated from the headers of a mysql table, i.e. there is name, order_id, webpage_url... and so on. I know I can select * from and iterate through results with an option statement echoed out but I don't want the data, just the names of the rows. Is that possible, if so can anyome give me a little insight please? Thank you in advance.
sspatel82
Forum Newbie
Posts: 9
Joined: Tue Jul 19, 2011 10:52 am

Re: dynamic dropdown

Post by sspatel82 »

ok....here is the mysql query to get all columns name for any table....

Code: Select all

SELECT * FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME='<YOUR TABLE NAME>';
use above query to get result and then create your dynamic drop down.
User avatar
McInfo
DevNet Resident
Posts: 1532
Joined: Wed Apr 01, 2009 1:31 pm

Re: dynamic dropdown

Post by McInfo »

If you pull the field names from INFORMATION_SCHEMA.COLUMNS, you must specify the TABLE_SCHEMA. Otherwise, the query will return field names from all databases that have a table with the specified name.

Code: Select all

SELECT `COLUMN_NAME` FROM `INFORMATION_SCHEMA`.`COLUMNS` WHERE `TABLE_SCHEMA` = 'my_database' AND `TABLE_NAME` = 'my_table'
Another way to access field names is with

Code: Select all

SHOW COLUMNS FROM `my_table`
Post Reply