-6

How do I display all field names from a MySQL table to a web page when I only know the table name?

Noel Vock
  • 287
  • 1
  • 3
  • 10
  • how do i answer a question when i only have one sentence to go on –  Aug 25 '14 at 22:03
  • Are you able to make a connection to mysql? – CChoma Aug 25 '14 at 22:03
  • 1
    possible duplicate of [Get table column names in mysql?](http://stackoverflow.com/questions/1526688/get-table-column-names-in-mysql) – Overachiever Aug 25 '14 at 22:05
  • 1
    Google search for "How do I display all field names from a MySQL table" would've yielded result #2 as [**this...** (Get table column names in mysql?)](http://stackoverflow.com/questions/1526688/get-table-column-names-in-mysql) which should have been marked as a duplicate. – Funk Forty Niner Aug 25 '14 at 22:06
  • The question also includes the part, 'How do I display all field names to a web page' and the links you provide do not do that. – Noel Vock Aug 25 '14 at 22:59
  • @CChoma Yes, i can connect to MySQL what I want is an efficient way od displaying the fieldnames to a web page – Noel Vock Aug 25 '14 at 23:41

2 Answers2

6

Use the information_schema meta-DB:

SELECT COLUMN_NAME
FROM information_schema.TABLES
WHERE (TABLE_SCHEMA='Yourdb') AND (TABLE_NAME = 'Yourtablename')
Marc B
  • 356,200
  • 43
  • 426
  • 500
2

In addition to @MarcB's suggestion, you can also do:

SHOW COLUMNS FROM table

http://dev.mysql.com/doc/refman/5.6/en/show-columns.html

ceejayoz
  • 176,543
  • 40
  • 303
  • 368
  • This would not work on a web page. It would return "array" as many times as there are columns. – Mark Dec 16 '14 at 20:08
  • @Mark WTF? Sure it'd work on a web page. You'd just have to write the right code to handle the data MySQL returns. – ceejayoz Dec 16 '14 at 20:27
  • Why not put that in your answer? – Mark Dec 16 '14 at 20:43
  • You mean where you insulted the author of this question? I'd like to see more productive comments from a user with 90k+ rep. – Mark Dec 16 '14 at 20:55