简体   繁体   中英

Get MySQL table columns in PHP

I want to find Mysql columns of a table in PHP.

This is my code but it does not work as expected:

 while($row3 = mysql_fetch_array($query3)) { foreach($row3 as $column => $data) { echo'<td>'.$column.'</td>'; } }

The table columns in the database are ID , User and Pass .

But the output is 0 ID 1 User 2 Pass

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;
   while ($row = mysql_fetch_assoc($result))

用这个。

Change mysql_fetch_array($query) to mysql_fetch_assoc($query).

It will give array of column name as a key and column's value as value.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM