Need to retrieve table FILEDS as well as data from mysql

Discussion in 'PHP' started by jacka, Jan 9, 2008.

  1. #1
    Hi
    I know how to retrieve data from mysql but I can't find out how to retrieve the fields from a table.

    I will have several tables each with different number of fields and names and need a php code to examine how many fields there are and retrieve them and display their content using a table with field headings.

    I have already got the code for table content, but how do I go about adding the name of the fields to the table heading?

    This my code so far:
    
    
    echo "<table border='1' align='center'> ";
    		echo "<tr> <th>Part Number</th> <th>Description</th><th >Price (&pound;)</th><th>Pdf</th> </tr>";
    		// keeps getting the next row until there are no more to get
    		
    		
    
    
    	
    		
    		while($row = mysql_fetch_array( $result )) { 
    			// Print out the contents of each row into a table 
    		  
    		
    	$pdf = mysql_query( " SELECT * FROM pn_quark
    WHERE part_number ='" . $row['part_number'] . "'") or die(mysql_error());
    		 $rowpdf = mysql_fetch_array($pdf);
    		 
    		
    		
    	
    		
    		
    		
    		
    			echo "<tr ><td align=\"center\">";  
    			echo $row['part_number']; 
    			echo "</td><td align=\"center\">"; 
    			echo $row['desc']; 
    			echo "</td><td align=\"center\">"; 
    			echo $row['price_each1']; 
    		echo "</td><td align=\"center\">"; 
    				echo "<a target=\"_blank\" href=\"http://www.xxxl.com/C27reduced/C27 
    			$rowpdf[page_number].pdf\"><img class=\"pdf\" src=\"http://www.xxxl.com/images/pdflogo.gif\"></a>";
    			
    		//	echo "<br/>";
    		//	echo " <a  class=\"report\" href=\"mailto:xx@xxx?subject=Feedback for hpcgears.com&body=Hi%0A %0A The following s Part Number produced the wrong pdf file.%0A %0A  Part Number:     \">Incorrect pdf? Report here.</a> ";
    			
    			echo "</td></tr>";   
    		} 
    HTML:
     
    jacka, Jan 9, 2008 IP
  2. HoagieKat

    HoagieKat Peon

    Messages:
    87
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    #2
    you can run through the array using a foreach($row as $k=>$v){ print($k); } loop which will output all the keys for the array.
     
    HoagieKat, Jan 9, 2008 IP
  3. jacka

    jacka Peon

    Messages:
    165
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Hi
    Thanks for your reply.

    What I forgot to say was that I am fairly new to php and mysql, so would be grateful if you would elaborate alittle bit.
    thanks
    jacka:confused:
     
    jacka, Jan 9, 2008 IP
  4. HoagieKat

    HoagieKat Peon

    Messages:
    87
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    #4
    alrighty.

    
    echo("<tr>");
    foreach($row as $k=>$v)
    {
         echo("<td>$k</td>");
    }
    echo("<tr>")
    
    Code (markup):
    will output a table entry that'll print all headers in a fetched mysql_row... I'll leave you to figure out the rest, otherwise you'll never learn. :D
     
    HoagieKat, Jan 10, 2008 IP
  5. jacka

    jacka Peon

    Messages:
    165
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #5
    Hi
    I have taken your hint and constructed this code.
    
    
    
    		
    		echo "<table border='1' align='center'> ";
    	
    
    		  
    		
    		while($row = mysql_fetch_array( $result )) { 
    		
    	
    		
    		
    		
    		
    					echo("<tr>");
    foreach($row as $k=>$v)
    {
         echo("<td>$k</td>");
    }
    echo("<tr>");
    
    
    			  
    		}
    PHP:
    But why it print extra 0,1 ,2 ,3 etc. in the heading?

    with the folllowing result
    
    0	id	1	part_number	2	basic_�d	3	min_�d	4	max(�d)	5	basic (�D)	6	min (�D)	7	max (�D)	8	basic_L	9	des
    
    
    
    HTML:
    Also how can I display Diameter sign ( its like a zero with a line through it), it comes up like a black diamond with a question mark inside it.
     
    jacka, Jan 10, 2008 IP
  6. jacka

    jacka Peon

    Messages:
    165
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #6
    Hi
    solved one problem:

    Change it to mysql_fetch_assoc and it worked.

    Any ideas about displaying diameter sign please?
     
    jacka, Jan 10, 2008 IP
  7. HoagieKat

    HoagieKat Peon

    Messages:
    87
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    #7
    change your page's encoding to UTF-8
     
    HoagieKat, Jan 10, 2008 IP