how return a field name with the value in a mysql query?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
srdva59
Forum Commoner
Posts: 77
Joined: Sun Feb 15, 2009 10:58 am

how return a field name with the value in a mysql query?

Post by srdva59 »

--------------------------------------------------------------------------------

hi,
i need get the name of the field by a query
for example

if a do this:

$res = mysql_query( "select * from total )
$v1=mysql_fetch_row_name($res);
while($v1=mysql_fetch_row($res)) {

echo $v1[0]; // return the value of field number 0

}

but i need to know also this:


echo "the field name xxx have this record ". $v1[0];

in vb we have the objects that return the name and the value and all information of the field but how can i see this in php?
thanks a lot for your help
User avatar
VladSun
DevNet Master
Posts: 4313
Joined: Wed Jun 27, 2007 9:44 am
Location: Sofia, Bulgaria

Re: how return a field name with the value in a mysql query?

Post by VladSun »

If you use mysql_fetch_assoc() it will return an associative array where keys are the names of the columns in you SQL query (notice - the way you name them in the query) and values - the values of these rows.
Then use

Code: Select all

foreach ($row as $field => $value)
to iterate it.
There are 10 types of people in this world, those who understand binary and those who don't
Post Reply