Solved

Display Column Name with PHP using MYSQL dbase

Posted on 2004-09-11
5
381 Views
Last Modified: 2013-12-12
I have mysql table and would like to display column names along data in vertical fashion. My PHP code is as follows where instead of static (dummy) FieldName I would like to dump column name and if possible replace underscore with space e.g (First_Name with First Name):

<?php
         for ( $counter= 0;
         $row = mysql_fetch_row( $result );
         $counter++ ){          
         foreach ( $row as $key => $value )
         print( "<tr><td>FieldName<td>$value</td></tr>" );              
      }
      mysql_close( $database );
?

at the moment my php displays:
FieldName    John
FieldName    Smith
FieldName    35
etc....

tks mates.
0
Comment
Question by:miamati
  • 4
5 Comments
 
LVL 27

Expert Comment

by:Diablo84
ID: 12033947
try:

         for ( $counter= 0;
         $row = mysql_fetch_row( $result );
         $counter++ ){          
         foreach ( $row as $key => $value )
         print "<tr><td>";
         echo mysql_field_name($result,$key);
         print "<td>$value</td></tr>";              
      }
      mysql_close( $database );
0
 
LVL 27

Expert Comment

by:Diablo84
ID: 12033966
I think your code will work better like this

      for ($counter= 0;$row = mysql_fetch_row($result);$counter++ ){
       foreach ($row as $key => $value) {       
          print "<tr><td>";
          echo mysql_field_name($result,$key);
          print "<td>$value</td></tr>";
       }
      }
      mysql_close( $database );
0
 
LVL 27

Expert Comment

by:Diablo84
ID: 12033976
oh and this accounts for replacing the underscore too:

 for ($counter=0;$row=mysql_fetch_row($result);$counter++){
  foreach ($row as $key => $value) {
   echo "<tr><td>";
   $field = mysql_field_name($result,$key);
   $field = str_replace("_"," ",$field);
   echo $field;
   echo "<td>$value</td></tr>\n";
  }
 }
 mysql_close($database);
0
 

Author Comment

by:miamati
ID: 12034027
ok many thanks..succeeded! Before I accept answer is there any way to code add to your last solution in order to convert first column name lower case to upper case? Eg. col surname to Surname or col work experience to Work experience. Thanks again.
0
 
LVL 27

Accepted Solution

by:
Diablo84 earned 500 total points
ID: 12034438
use the ucfirst function, eg.

 for ($counter=0;$row=mysql_fetch_row($result);$counter++){
  foreach ($row as $key => $value) {
   echo "<tr><td>";
   $field = mysql_field_name($result,$key);
   $field = str_replace("_"," ",$field);
   echo ucfirst($field);
   echo "<td>$value</td></tr>\n";
  }
 }
 mysql_close($database);
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Author Note: Since this E-E article was originally written, years ago, formal testing has come into common use in the world of PHP.  PHPUnit (http://en.wikipedia.org/wiki/PHPUnit) and similar technologies have enjoyed wide adoption, making it possib…
Part of the Global Positioning System A geocode (https://developers.google.com/maps/documentation/geocoding/) is the major subset of a GPS coordinate (http://en.wikipedia.org/wiki/Global_Positioning_System), the other parts being the altitude and t…
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

749 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question