How to obtain column comments from SQL - php

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!

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

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.

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'

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.

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)

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

Related

Search In complete database

I like to build a query which can search in complete data base in all tables.
Ex.
Search String: 9049
Now in result I need search result from all table where this string matches.
Simply I can't write LIKE to all field of table.
Thanks In Advance.
You could do an SQLDump of the database (and its data) then search that file.
or
You can peek into the information_schema table. It has a list of all tables and all fields that are in a table. You can then run queries using the information that you have gotten from this table.
You could first read out all the tables:
SELECT table_name FROM information_schema.tables WHERE table_schema = 'yourdatabasename'
and then loop through all the tables and get all the columns:
SELECT column_name FROM information_schema.columns
WHERE table_schema = 'yourdatabasename' AND table_name = 'thetablenamefromyourloop'
and then build a dynamic statement thats builds the WHERE clause based on your searchterm.
or to get just all the information just read out everything. This contains all the information about all the tables and columns, which might be useful to just search in all the varchar and text fields etc.
SELECT * FROM information_schema.columns
WHERE table_schema = 'yourdatabasename'

How to get MySQL table column comment text in PHP? [duplicate]

When you are creating a field in a MySQL table, there's a 'Comments' box to fill out. How can I access the data in that 'Comments' box with PHP?
Use:
SHOW FULL COLUMNS FROM tbl_name
Notice keyword FULL, this is what makes MySQL to include privileges and comments info into the response.
Poke around information_schema.
SELECT table_comment
FROM information_schema.tables
WHERE table_schema = 'myschema' AND table_name = 'mytable'
SELECT
COLUMN_COMMENT
FROM
information_schema.COLUMNS
WHERE
TABLE_NAME = 'venue'
Great to use for table column headings or more readable/secure version of real column names. Ex., The column for Venue ID is actually vid_xt
Sample Result from actual query above:
COLUMN_COMMENT
Venue ID
Venue Active
Venue Name
Location Name
Address
Accommodations
Description
Ah, wow. So that's what information_schema database is for. Thank you #Adam Backstrom! Then I believe below should give me the field's comments.
SELECT COLUMN_COMMENT
FROM information_schema.COLUMNS
WHERE TABLE_SCHEMA = 'mydatabase' AND TABLE_NAME = 'mytable' AND COLUMN_NAME = 'mycolumn'
Thank you for pointing me to the right direction. :-)
I discovered that the following query does the trick for me:
select column_comment from information_schema.columns
where table_schema = "YOUR_SCHEMA"
and table_name = "YOUR_TABLE";

How To Just Print Columns Names From a MySQL Database

How do you print out all the column names of a table. Just like a simple, for loop type printing out. I'm trying a wacky thing and checking against the column names will be useful.
This may sound like a similar question to others, but either I'm too stupid or I just haven't found my answer.
Simplifcation - If there are 3 columns in the table, I want to just print them out like
column1
column2
column3
More Explanation I should have added before. I'm using php to run a website that stores data into a database. I want to try a theory that involves using the column names of a table in order to change the values in that table.
#Michael -
$SQL = "SELECT column_name from information_schema.columns where
table_schema = "db1.com" and table_name = "users" ";
Thank you all for your help. However I have given up on trying this method. Thank you for all the responses
SELECT `COLUMN_NAME`
FROM `INFORMATION_SCHEMA`.`COLUMNS`
WHERE `TABLE_SCHEMA`='yourdatabasename'
AND `TABLE_NAME`='yourtablename';
SELECT column_name from information_schema.columns where table_schema = "your_database_name" and table_name = "your_table_name";
Actually the answer is the longer one. The right answer for this is describe {table_name}

Access MySQL field's Comments with PHP

When you are creating a field in a MySQL table, there's a 'Comments' box to fill out. How can I access the data in that 'Comments' box with PHP?
Use:
SHOW FULL COLUMNS FROM tbl_name
Notice keyword FULL, this is what makes MySQL to include privileges and comments info into the response.
Poke around information_schema.
SELECT table_comment
FROM information_schema.tables
WHERE table_schema = 'myschema' AND table_name = 'mytable'
SELECT
COLUMN_COMMENT
FROM
information_schema.COLUMNS
WHERE
TABLE_NAME = 'venue'
Great to use for table column headings or more readable/secure version of real column names. Ex., The column for Venue ID is actually vid_xt
Sample Result from actual query above:
COLUMN_COMMENT
Venue ID
Venue Active
Venue Name
Location Name
Address
Accommodations
Description
Ah, wow. So that's what information_schema database is for. Thank you #Adam Backstrom! Then I believe below should give me the field's comments.
SELECT COLUMN_COMMENT
FROM information_schema.COLUMNS
WHERE TABLE_SCHEMA = 'mydatabase' AND TABLE_NAME = 'mytable' AND COLUMN_NAME = 'mycolumn'
Thank you for pointing me to the right direction. :-)
I discovered that the following query does the trick for me:
select column_comment from information_schema.columns
where table_schema = "YOUR_SCHEMA"
and table_name = "YOUR_TABLE";

Recieving only the structure of the table using SQL and PHP and put into an Array

Is it possible to receive only the structure of the table even if its empty and put the field names in an array. If so which SQL command makes that possible.
If you are using MySQL 5.0 or later, you can get the field names from the INFORMATION_SCHEMA.COLUMNS table.
Something like
SELECT COLUMN_NAME
FROM COLUMNS
WHERE TABLE_NAME = <table_name>
Here is a link to a list of tables in the INFORMATION_SCHEMA database.
In Oracle and MySQL, this SQL query will give you the details of the table, including columns and column types:
describe table_name
This may or may not work in other databases.
thanks for the link the awnser is for example table 'vraag'
SELECT COLUMN_NAME
FROM INFORMATION_SCHEMA.COLUMNS
WHERE table_name = 'vraag'

Categories