1. Computer problem? Tech Support Guy is completely free -- paid for by advertisers and donations. Click here to join today! If you're new to Tech Support Guy, we highly recommend that you visit our Guide for New Members.

PHP & MySQL: How do I display a table from a query without hardcoding field names?

Discussion in 'Web Design & Development' started by adsmith82, Apr 4, 2008.

Thread Status:
Not open for further replies.
Advertisement
  1. adsmith82

    adsmith82 Thread Starter

    Joined:
    Jul 19, 2006
    Messages:
    181
    How do I display a table from a query without hardcoding field names?
     
  2. adsmith82

    adsmith82 Thread Starter

    Joined:
    Jul 19, 2006
    Messages:
    181
    or rather how do i retrieve the field names after having selected a table
     
  3. TheRobatron

    TheRobatron

    Joined:
    Oct 25, 2007
    Messages:
    551
    Have you tried "SHOW COLUMNS FROM my_table"?
     
  4. Big-K

    Big-K

    Joined:
    Nov 22, 2003
    Messages:
    6,052
    another possibility is this, it just popped in my head now so i havn't personally tried it, but I can't see any reason it wouldnt work

    PHP:
    $query mysql_query("SELECT * FROM my_table");
    while(
    $row mysql_fetch_array($query)){
    echo 
    "In this row...<br />";
    foreach(
    $row as $key=>$field)
    echo 
    $key." has a value of ".$field."<br />";
    }
    but if all you need is the names, not the data, then just use show columns
     
As Seen On
As Seen On...

Welcome to Tech Support Guy!

Are you looking for the solution to your computer problem? Join our site today to ask your question. This site is completely free -- paid for by advertisers and donations.

If you're not already familiar with forums, watch our Welcome Guide to get started.

Join over 733,556 other people just like you!

Loading...
Thread Status:
Not open for further replies.

Short URL to this thread: https://techguy.org/700429

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice