Results 1 to 5 of 5

Thread: Displaying the column names of a table on a page

  1. #1
    Join Date
    Oct 2005
    Posts
    82

    Displaying the column names of a table on a page

    I need to know on how to display on a page the column names of a table . By using Show is there a way to read that info and then to to display that info again? Or in a separate table can i store these info & the display these values?

    I tested it but it is not working -
    Code:
    $result = mysql_query ("show columns from industries"); 
    
    print ("<table>");
    if ($row = mysql_fetch_array($result)) { 
    do { 
    print ("<tr><td bgcolor=#cccccc>");
    print $row["field"]; 
    print ("</td><td bgcolor=#cccc00>"); 
    print $row["type"]; 
    print ("</td><td bgcolor=#cccc00>"); 
    print $row["null"]; 
    print ("</tr>"); } 
    while($row = mysql_fetch_array($result)); } 
    else {print "Sorry, no records were found!";} 
    
    print ("</table>");
    Any help would be appreciated

  2. #2
    Join Date
    Jun 2006
    Posts
    623
    Have you seen the manual for these functions:

    Code:
    mysql_list_fields()
    mysql_field_name()

  3. #3
    Join Date
    Dec 2007
    Posts
    1,599
    To show the fields you can use the SHOW FIELDS from {tablename} syntax, or keys to your arrays can be just used by you, cause they are keyed by the field names you wanna select. This is not exact but something like this will work.

    PHP Code:
    $rs mysql_query($some_sql); 
    $printed_headers false
    echo 
    "<table border=2>"

    while ( 
    $row mysql_fetch_array($rs) ) 

        if ( !
    $printed_headers 
        { 
            
    //print the headers once: 
            
    echo "<tr>"
            foreach ( 
    array_keys($row) AS $header 
            { 
                
    //you have integer keys as well as string keys because of the way PHP  
                //handles arrays. 
                
    if ( !is_int($header) ) 
                { 
                    echo 
    "<th>$header</th>"
                } 
            } 
            echo 
    "</tr>"
            
    $printed_headers true
        } 
         
        
    //print the data row 
        
    echo "<tr>"
        foreach ( 
    $row AS $key=>$value 
        { 
            if ( !
    is_int($key) ) 
            { 
                echo 
    "<td>$value</td>"
            } 
        } 
        echo 
    "</tr>"

    echo 
    "</table>"

  4. #4
    Join Date
    Dec 2007
    Posts
    1,547
    PHP Code:
    //you have integer keys as well as string keys because of the way PHP  
           //handles arrays. 


    You should use mysql_fetch_assoc() - http://www.php.net/mysql_fetch_assoc if you want to avoid the step caue you have numeric keys as you are getting the result through mysql_fetch_array() - http://www.php.net/mysql_fetch_array

  5. #5
    Join Date
    Dec 2007
    Posts
    1,599
    I think that something might be wrong in my example caue i had that problem with different version of the fetch commands. Nice tip Thanks & he can foolow up your method by removing both of the if ( !is_int() ) from my code that i made.

Similar Threads

  1. What is the way to sum up number of column in Pivot Table
    By Dvimida in forum MS Office Support
    Replies: 2
    Last Post: 09-02-2012, 07:31 PM
  2. Is it possible to modify Excel Column Names
    By gUILl in forum MS Office Support
    Replies: 2
    Last Post: 27-01-2012, 08:08 PM
  3. Replies: 10
    Last Post: 03-12-2011, 10:31 AM
  4. How to add new column in table in sql server
    By MKAIF in forum Software Development
    Replies: 4
    Last Post: 02-02-2010, 08:34 PM
  5. Determining table's column name in sql server
    By Jinendra in forum Windows Software
    Replies: 3
    Last Post: 10-06-2009, 12:14 AM

Tags for this Thread

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
  •  
Page generated in 1,713,899,684.44170 seconds with 17 queries