Column names of certain fields

Questions about the MySQL, PostgreSQL, and most other databases, as well as using it with PHP can be asked here.

Moderator: General Moderators

Post Reply
jpaw
Forum Newbie
Posts: 1
Joined: Mon Feb 27, 2006 9:32 pm

Column names of certain fields

Post by jpaw »

Is there an easy way to display the column name(s) from a mysql result? For example, if I have an SQL statement like

Code: Select all

SELECT id,name FROM `table`
and assuming that I do not know the SQL statement beforehand, is there a way to find the column name and display it?

I know

Code: Select all

SHOW FIELDS FROM
will show all of the fields in a table, but I just need to display the ones used in the query.

Sorry if what I'm trying to say isn't clear, but that's the best way I can think of stating it right now.

Thanks in advance.
User avatar
Benjamin
Site Administrator
Posts: 6935
Joined: Sun May 19, 2002 10:24 pm

Post by Benjamin »

Code: Select all

$Data = mysql_fetch_assoc(mysql_query("select * from table",$LinkID));
Returns an array with the field names as keys and the corresponding values.
josh
DevNet Master
Posts: 4872
Joined: Wed Feb 11, 2004 3:23 pm
Location: Palm beach, Florida

Post by josh »

To add onto that

Code: Select all

print_r(array_keys($Data));
Post Reply