mysql查找表shoe table,在mysql中獲取表列名?

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

有沒有辦法在mysql中獲取表的列名?使用php

17 个解决方案

#1

403

You can use DESCRIBE:

您可以使用描述:

DESCRIBE my_table;

Or in newer versions you can use INFORMATION_SCHEMA:

或者在新版本中,你可以使用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:

SHOW COLUMNS FROM my_table;

#2

32

The following SQL statements are nearly equivalent:

下面的SQL語句幾乎是等價的:

SELECT COLUMN_NAME

FROM INFORMATION_SCHEMA.COLUMNS

WHERE table_name = 'tbl_name'

[AND table_schema = 'db_name']

[AND column_name LIKE 'wild']

SHOW COLUMNS

FROM tbl_name

[FROM db_name]

[LIKE 'wild']

參考:INFORMATION_SCHEMA列

#3

19

I made a PDO function which returns all the column names in an simple array.

我做了一個PDO函數,它返回一個簡單數組中的所有列名。

public function getColumnNames($table){

$sql = "SELECT COLUMN_NAME FROM INFORMATION_SCHEMA.COLUMNS WHERE table_name = :table";

try {

$core = Core::getInstance();

$stmt = $core->dbh->prepare($sql);

$stmt->bindValue(':table', $table, PDO::PARAM_STR);

$stmt->execute();

$output = array();

while($row = $stmt->fetch(PDO::FETCH_ASSOC)){

$output[] = $row['COLUMN_NAME'];

}

return $output;

}

catch(PDOException $pe) {

trigger_error('Could not connect to MySQL database. ' . $pe->getMessage() , E_USER_ERROR);

}

}

The output will be an array:

輸出將是一個數組:

Array (

[0] => id

[1] => name

[2] => email

[3] => shoe_size

[4] => likes

... )

Sorry for the necro but I like my function ;)

為necro對不起,但我喜歡我的功能;

P.S. I have not included the class Core but you can use your own class.. D.S.

附注:我沒有包含類的核心,但是你可以使用你自己的類。科學博士

#4

5

This solution is from command line mysql

這個解決方案來自命令行mysql

mysql>USE information_schema;

In below query just change to your database and to your table name where you just want Field values of DESCRIBE statement

在下面的查詢中,只需將更改為您的數據庫,並將更改為您的表名,您只需要描述語句的字段值

mysql> SELECT COLUMN_NAME FROM COLUMNS WHERE TABLE_SCHEMA = '' AND TABLE_NAME='';

#5

5

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;

#6

3

Look into:

調查:

mysql_query('DESCRIBE '.$table);

#7

3

The MySQL function describe table should get you where you want to go (put your table name in for "table"). You'll have to parse the output some, but it's pretty easy. As I recall, if you execute that query, the PHP query result accessing functions that would normally give you a key-value pair will have the column names as the keys. But it's been a while since I used PHP so don't hold me to that. :)

MySQL函數description table應該可以得到您想要的位置(將您的表名放在“table”中)。您將不得不解析輸出,但這非常簡單。我記得,如果您執行這個查詢,PHP查詢結果訪問通常會給您一個鍵值對的函數將以列名作為鍵。但是我使用PHP已經有一段時間了,所以不要逼我這么做。:)

#8

3

There's also this if you prefer:

如果你喜歡,也可以這樣:

mysql_query('SHOW COLUMNS FROM tableName');

#9

3

You may also want to check out mysql_fetch_array(), as in:

您可能還想檢查mysql_fetch_array(),如:

$rs = mysql_query($sql);

while ($row = mysql_fetch_array($rs)) {

//$row[0] = 'First Field';

//$row['first_field'] = 'First Field';

}

#10

3

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.

同樣有趣的是,您可以使用EXPLAIN table_name,它是描述table_name的同義詞,並且可以從table_name顯示列,盡管解釋更常用來獲取關於查詢執行計划的信息。

#11

2

The mysql_list_fields function might interest you ; but, as the manual states :

您可能對mysql_list_fields函數感興趣;但是,正如手冊所說:

This function is deprecated. It is preferable to use mysql_query() to issue a SQL SHOW COLUMNS FROM table [LIKE 'name'] statement instead.

這個函數是棄用。最好使用mysql_query()從表[比如'name']語句中發出SQL SHOW列。

#12

2

I needed column names as a flat array, while the other answers returned associative arrays, so I used:

我需要將列名作為一個平面數組,而其他答案返回關聯數組,所以我使用:

$con = mysqli_connect('localhost',$db_user,$db_pw,$db_name);

$table = 'people';

/**

* Get the column names for a mysql table

**/

function get_column_names($con, $table) {

$sql = 'DESCRIBE '.$table;

$result = mysqli_query($con, $sql);

$rows = array();

while($row = mysqli_fetch_assoc($result)) {

$rows[] = $row['Field'];

}

return $rows;

}

$col_names = function get_column_names($con, $table);

$col_names now equals:

現在美元col_names等於:

(

[0] => name

[1] => parent

[2] => number

[3] => chart_id

[4] => type

[5] => id

)

#13

1

this worked for me..

這個工作對我來說. .

$sql = "desc MyTableName";

$result = @mysql_query($sql);

while($row = @mysql_fetch_array($result)){

echo $row[0]."
";

}

#14

1

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

您可以使用以下簡單命令獲得整個表結構。

DESC TableName

or you can use following query.

或者可以使用以下查詢。

SHOW COLUMNS FROM TableName

#15

1

in mysql to get columns details and table structure by following keywords or queries

在mysql中,通過以下關鍵字或查詢獲取列細節和表結構

1.DESC table_name

1。DESC table_name

2.DESCRIBE table_name

2。描述table_name

3.SHOW COLUMNS FROM table_name

3所示。從table_name顯示列

4.SHOW create table table_name;

4所示。顯示創建表table_name;

5.EXPLAIN table_name

5。解釋table_name

#16

1

$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);

}

#17

1

I have write a simple php script to fetch table columns through PHP: Show_table_columns.php

我編寫了一個簡單的php腳本,通過php: Show_table_columns.php獲取表列

Enjoy!

享受吧!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值