Page 1 of 1

dynamic dropdown

Posted: Tue Jul 19, 2011 10:35 pm
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.

Re: dynamic dropdown

Posted: Tue Jul 19, 2011 10:49 pm
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.

Re: dynamic dropdown

Posted: Wed Jul 20, 2011 5:05 pm
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`