Get table column names in MySQL?

2019-01-01 04:50发布

Is there a way to grab the columns name of a table in mysql? using php

标签: php sql mysql
17条回答
千与千寻千般痛.
2楼-- · 2019-01-01 05:22

It's also interesting to note that you can use
EXPLAIN table_name which is synonymous with DESCRIBE table_name and SHOW COLUMNS FROM table_name although EXPLAIN is more commonly used to obtain information about the query execution plan.

查看更多
ら面具成の殇う
3楼-- · 2019-01-01 05:22
$col = $db->query("SHOW COLUMNS FROM category");

while ($fildss = $col->fetch_array())
{             
    $filds[] = '"{'.$fildss['Field'].'}"';
    $values[] = '$rows->'.$fildss['Field'].'';
}

if($type == 'value')
{
    return $values = implode(',', $values);
}
else {
     return $filds = implode(',', $filds);
}
查看更多
旧人旧事旧时光
4楼-- · 2019-01-01 05:23

Look into:

mysql_query('DESCRIBE '.$table);
查看更多
其实,你不懂
5楼-- · 2019-01-01 05:25

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;
查看更多
倾城一夜雪
6楼-- · 2019-01-01 05:25

you can get the entire table structure using following simple command.

DESC TableName

or you can use following query.

SHOW COLUMNS FROM TableName
查看更多
临风纵饮
7楼-- · 2019-01-01 05:27

How about this:

SELECT @cCommand := GROUP_CONCAT( COLUMN_NAME ORDER BY column_name SEPARATOR ',\n')
FROM INFORMATION_SCHEMA.COLUMNS 
WHERE TABLE_SCHEMA = 'my_database' AND TABLE_NAME = 'my_table';

SET @cCommand = CONCAT( 'SELECT ', @cCommand, ' from my_database.my_table;');
PREPARE xCommand from @cCommand;
EXECUTE xCommand;
查看更多
登录 后发表回答