Home > Backend Development > PHP Tutorial > php如何样获取mysql表中所有字段名到一个数组

php如何样获取mysql表中所有字段名到一个数组

WBOY
Release: 2016-06-13 13:03:33
Original
1059 people have browsed it

php怎么样获取mysql表中所有字段名到一个数组?
php怎么样获取mysql表中所有字段名到一个数组?
select * from table 虽然能做到,但是返回的是array('name'=>'hello','age'=>30,...)这样的数组,但我只需要array('name','age');
有其他更快的方法吗?
------最佳解决方案--------------------
首先 用select * from table limit(0,10) 查出所有数据到一个数组,格式为array('name'=>'hello','age'=>30,...),然后对该数组使用array_keys()函数 ,即可得到你要的格式array('name','age',......);
------其他解决方案--------------------
这样写

$result = mysql_query("SELECT * FROM table");<br />
$fields = mysql_num_fields($result);<br />
for ($i=0; $i < $fields; $i++) {<br />
      $names[]  = mysql_field_name($result, $i);<br />
}<br />
print_r($names);
Copy after login

------其他解决方案--------------------
SELECT  COLUMN_NAME  FROM  `information_schema`.`COLUMNS`   where `TABLE_SCHEMA`='你的书库库名'  and  `TABLE_NAME`='你的表名'   order by COLUMN_NAME;

改改就能用了
------其他解决方案--------------------
select name,age from table
------其他解决方案--------------------
foreach试试呢

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template