retrieving database fields name

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
naeem1984
Forum Newbie
Posts: 10
Joined: Thu Feb 26, 2009 3:55 am

retrieving database fields name

Post by naeem1984 »

i retrieve the data from database using following code

Code: Select all

 
while ($row = (mysql_fetch_array($result1)))
   {
        
        echo $row["auto#"]."<br>";
        echo $row["name"]."<br>";
        echo $row["email"]."<br>";
   
   }
 
but i want to retrieve the database fields name from database which are
auto#,
name,
email
User avatar
jayshields
DevNet Resident
Posts: 1912
Joined: Mon Aug 22, 2005 12:11 pm
Location: Leeds/Manchester, England

Re: retrieving database fields name

Post by jayshields »

I don't really understand why you'd need to do this. Is it dynamic query generation or something? You could just use array_keys() on $row inside the while loop.

Maybe a DESCRIBE query is what you're after.
User avatar
VladSun
DevNet Master
Posts: 4313
Joined: Wed Jun 27, 2007 9:44 am
Location: Sofia, Bulgaria

Re: retrieving database fields name

Post by VladSun »

Code: Select all

while ($row = (mysql_fetch_array($result1)))
        foreach ($row as $key => $value)
              echo $key . " =  " . $value . "<br />";
PS: jayshields is right - you need to clarify your question.
There are 10 types of people in this world, those who understand binary and those who don't
naeem1984
Forum Newbie
Posts: 10
Joined: Thu Feb 26, 2009 3:55 am

Re: retrieving database fields name

Post by naeem1984 »

how avoid indexes which shows with keys and values

Code: Select all

 
foreach ($row as $key => $value)
    {
           echo  "<td>".$key ."</td>";
    }
 
it shows the field name with indexes
e.g
0 auto#
1 name
2 email
3 person

how avoid indexes
User avatar
VladSun
DevNet Master
Posts: 4313
Joined: Wed Jun 27, 2007 9:44 am
Location: Sofia, Bulgaria

Re: retrieving database fields name

Post by VladSun »

mysql_fetch_assoc()
There are 10 types of people in this world, those who understand binary and those who don't
Post Reply