SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    ********* Wizard silver trophy Cam's Avatar
    Join Date
    Aug 2002
    Location
    Burpengary, Australia
    Posts
    4,495
    Mentioned
    0 Post(s)
    Tagged
    1 Thread(s)

    Getting MySQL field names

    Hey,

    How can I get the field names from a MySQL table? Heres some pseudocode
    PHP Code:
    $sql "GET TABLE NAMES
              FROM tablename"
    ;
    $rs mysql_query($sql);
    $foo mysql_fetch_array();

    foreach ( 
    $foo as $val ) {
       echo 
    "Field name: {$val}<br />\n";

    Any ideas?

    Thanks

  2. #2
    if($awake){code();} PHP John's Avatar
    Join Date
    Jul 2002
    Location
    Along the Wasatch Fault line.
    Posts
    1,771
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    John

  3. #3
    ********* Wizard silver trophy Cam's Avatar
    Join Date
    Aug 2002
    Location
    Burpengary, Australia
    Posts
    4,495
    Mentioned
    0 Post(s)
    Tagged
    1 Thread(s)
    Thanks for that, works a treat

    I changed some of the code so it only populates the array with the field names and nothing else. Just posting it if anyones interested.
    PHP Code:
    $sql "DESCRIBE [tablename]" ;
    $rs mysql_query($sql);

    while ( 
    $row mysql_fetch_array($rs) ) {
        foreach ( 
    $row as $field ) {
            
    $dbField[] = $field;
            break;
        }
    }
    for ( 
    $i 0$i sizeof($dbField); $i++ ) {
        echo 
    "{$dbField[$i]}<br />\n";



Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •