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?