Is there a way to grab the columns name of a table in MySQL using PHP?
To get the column name of a table we use sp_help with the name of the object or table name. sp_columns returns all the column names of the object. The following query will return the table's column names: sp_columns @table_name = 'News'
To list all columns in a table, we can use the SHOW command. Let us first create a table. Syntax to list all column names.
SELECT table_name, column_name from information_schema. columns WHERE column_name LIKE '%column_name_to_search%'; Remember, don't use % before column_name_to_search if you know the starting characters of that column.
In SQL Server, you can select COLUMN_NAME from INFORMATION_SCHEMA. COLUMNS .
You can use DESCRIBE:
DESCRIBE my_table;
Or in newer versions you can use INFORMATION_SCHEMA:
SELECT COLUMN_NAME FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_SCHEMA = 'my_database' AND TABLE_NAME = 'my_table';
Or you can use SHOW COLUMNS:
SHOW COLUMNS FROM my_table;
Or to get column names with comma in a line:
SELECT group_concat(COLUMN_NAME) FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_SCHEMA = 'my_database' AND TABLE_NAME = 'my_table';
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With