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>$val ue</td></t r>" );
}
mysql_close( $database );
?
at the moment my php displays:
FieldName John
FieldName Smith
FieldName 35
etc....
tks mates.
<?php
for ( $counter= 0;
$row = mysql_fetch_row( $result );
$counter++ ){
foreach ( $row as $key => $value )
print( "<tr><td>FieldName<td>$val
}
mysql_close( $database );
?
at the moment my php displays:
FieldName John
FieldName Smith
FieldName 35
etc....
tks mates.
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 );
for ($counter= 0;$row = mysql_fetch_row($result);$
foreach ($row as $key => $value) {
print "<tr><td>";
echo mysql_field_name($result,$
print "<td>$value</td></tr>";
}
}
mysql_close( $database );
oh and this accounts for replacing the underscore too:
for ($counter=0;$row=mysql_fet ch_row($re sult);$cou nter++){
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);
for ($counter=0;$row=mysql_fet
foreach ($row as $key => $value) {
echo "<tr><td>";
$field = mysql_field_name($result,$
$field = str_replace("_"," ",$field);
echo $field;
echo "<td>$value</td></tr>\n";
}
}
mysql_close($database);
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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
for ( $counter= 0;
$row = mysql_fetch_row( $result );
$counter++ ){
foreach ( $row as $key => $value )
print "<tr><td>";
echo mysql_field_name($result,$
print "<td>$value</td></tr>";
}
mysql_close( $database );