Convert database rows to columns using PHP

This code will convert (pivot) your MySQL database table from rows to columns. Here is an example of what this code will do:

Original Table
IDNameColor
1JamesBlue
2AmyRed

 

Converted Table
ID12
NameJamesAmy
ColorBlueRed


n";
$records = array();
while(($row = mysql_fetch_assoc($result))) {
$records[]=$row;
}

foreach($records as $key => $row) {
foreach($row as $field => $value) {
$recNew[$field][] = $value;
}
}
foreach ($recNew as $key => $values)
{
echo "

n";
echo "t

" . $key . "

n";
foreach ($values as $cell)
{
echo "t

" . $cell . "

n";
}
echo "

n"; // end row
}
echo "

";

?>