Go Back   TechArena Community > Software > Software Development
Become a Member!
Forgot your username/password?
Tags Active Topics RSS Search Mark Forums Read

Reply
 
Thread Tools Search this Thread
  #1  
Old 04-08-2008
Member
 
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
Reply With Quote
  #2  
Old 04-08-2008
Member
 
Join Date: Jun 2006
Posts: 620
Have you seen the manual for these functions:

Code:
mysql_list_fields()
mysql_field_name()
Reply With Quote
  #3  
Old 04-08-2008
Member
 
Join Date: Dec 2007
Posts: 1,592
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>"
Reply With Quote
  #4  
Old 04-08-2008
Member
 
Join Date: Dec 2007
Posts: 1,542
Quote:
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
Reply With Quote
  #5  
Old 04-08-2008
Member
 
Join Date: Dec 2007
Posts: 1,592
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.
Reply With Quote
Reply

  TechArena Community > Software > Software Development
Tags: ,



Thread Tools Search this Thread
Search this Thread:

Advanced Search


Similar Threads for: "Displaying the column names of a table on a page"
Thread Thread Starter Forum Replies Last Post
What is the way to sum up number of column in Pivot Table Dvimida MS Office Support 2 09-02-2012 07:31 PM
Is it possible to modify Excel Column Names gUILl MS Office Support 2 27-01-2012 08:08 PM
IE 9 is displaying blank page and showing links as mouse hovered on the page Chitti's Technology & Internet 10 03-12-2011 10:31 AM
How to add new column in table in sql server MKAIF Software Development 4 02-02-2010 08:34 PM
Determining table's column name in sql server Jinendra Windows Software 3 10-06-2009 12:14 AM


All times are GMT +5.5. The time now is 04:49 AM.