How does php obtain all the field names in the mysql table to an array?

Source: Internet
Author: User
How does php obtain the names of all fields in the mysql table to an array? How does php obtain the names of all fields in the mysql table to an array? Select & nbsp; * & nbsp; from & nbsp; table & nbsp; The Returned array ('name' = & gt; 'hello ', 'age' = & gt; 30 ,...) how can I get all the field names in the mysql table to an array in php?
How does php obtain the names of all fields in the mysql table to an array?
Although select * from table can do this, array ('name' => 'hello', 'age' => 30,...) is returned ,...) this array, but I only need array ('name', 'age ');
Are there other faster methods?
------ Best solution --------------------
First, use select * from table limit () to find all data to an array in the format of array ('name' => 'hello ', 'age' => 30 ,...), then, use the array_keys () function for the array to obtain the desired format array ('name', 'age ',......);
------ Other solutions --------------------
Write in this way
$result = mysql_query("SELECT * FROM table");
$fields = mysql_num_fields($result);
for ($i=0; $i < $fields; $i++) {
      $names[]  = mysql_field_name($result, $i);
}
print_r($names);

------ Other solutions --------------------
SELECT COLUMN_NAME FROM 'information _ scheme '. 'columns 'where' TABLE _ scheme' = 'name of your library 'and 'Table _ name' = 'name of your TABLE 'Order by COLUMN_NAME;

Change can be used.
------ Other solutions --------------------
Select name, age from table
------ Other solutions --------------------
How about foreach?
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.