Solved

Display Column Name with PHP using MYSQL dbase

Posted on 2004-09-11
5
376 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

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

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

Suggested Solutions

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…
Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

776 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