Link to home
Start Free TrialLog in
Avatar of miamati
miamati

asked on

Display Column Name with PHP using MYSQL dbase

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.
Avatar of Diablo84
Diablo84

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 );
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 );
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);
Avatar of miamati

ASKER

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.
ASKER CERTIFIED SOLUTION
Avatar of Diablo84
Diablo84

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial