4

As you guys propably know, there's the possibility to add a comment to a column in MySQL. Now I was wondering how I could obtain this comment via PHP/MySQL. I was searching the web but I didn't find any solution yet. Do you guys have any idea/solution for this problem?

Greetings!

n0pt3x
  • 117
  • 2
  • 2
  • 6
  • @Chris, that link is for postgresql, and it does not mention `information_schema` how is that useful? – Johan Jul 19 '11 at 18:36

6 Answers6

13
SELECT
    COLUMN_COMMENT
FROM
    INFORMATION_SCHEMA.COLUMNS
WHERE
    TABLE_SCHEMA = 'db-name' AND
    TABLE_NAME = 'table-name' AND
    COLUMN_NAME = 'column-name'

http://dev.mysql.com/doc/refman/5.0/en/columns-table.html

Hammerite
  • 20,746
  • 4
  • 67
  • 87
8

Just use this SQL:

SHOW FULL COLUMNS FROM myTable

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

The FULL keyword causes the output to include the column collation and comments, as well as the privileges you have for each column.

AlienWebguy
  • 75,484
  • 17
  • 116
  • 141
2

Data regarding MySQL's tables in stored in the information_schema views.
You should be able to get it from there. This requires root privileges.

SELECT table_schema, table_name, column_comment 
FROM INFORMATION_SCHEMA.`columns` c
WHERE c.table_schema = 'mydatabase' 
  AND c.table_name = 'mytable'
  AND c.column_name = 'myfield'
Johan
  • 73,011
  • 23
  • 185
  • 311
1

You can fetch those metadata from the information_schema database.

Ex:

SELECT column_name, column_comment FROM information_schema.columns WHERE table_name = 'user'

Where user is your table name.

Boris Guéry
  • 46,636
  • 7
  • 51
  • 86
1

If you have correct privileges you could make this query:

$query = "SHOW FULL COLUMNS from node;";

$result = mysql_query($query);

And then fetch the results (there is a column named Comment that holds the comments)

Nicola Peluchetti
  • 74,514
  • 30
  • 136
  • 188
0

Use the SQL command SHOW FULL COLUMNS as described in the MySQL manual. Its output contains the comments.

Steffen Müller
  • 2,220
  • 13
  • 18