如何使用mysql将行更改为cloumn

时间:2019-06-19 02:23:24

标签: php mysql

早上好,我发现很多例子,但仍然无法显示我想要的输出,请帮助我,谢谢 。现在这样输出:output 。我想要这样的输出:output

<?php
$servername = "localhost";$username = 'root';$password = "";$database = "ocall";
$mysqli = new mysqli("localhost", $username, $password, $database);$query = "SELECT * FROM timetable";

echo '<table border="1" ><tr><td><b> <font face="Arial">Day</font></td><td><b><font face="Arial">Date</font></td></tr>';

if ($result = $mysqli->query($query)) {while ($row = $result->fetch_assoc()) {$field1name = $row["Day"];$field2name = $row["Date"];

echo '
<tr><td>'.$field1name.'</td><td>'.$field2name.'</td></tr>';}$result->free();}
?>

1 个答案:

答案 0 :(得分:0)

一种方法是将值存储在单独的数组中。请尝试以下代码。

<?php
$servername = "localhost";
$username = 'root';
$password = "";
$database = "ocall";
$conn = new mysqli("localhost", $username, $password, $database);
if(!$conn){echo "Error";}
$query = "SELECT * FROM `timetable`";
$result = mysqli_query($conn, $query);

while($row = mysqli_fetch_assoc($result)){
    $daytime[] =  $row['Day'] ;
    $dates[] = $row['Date'];
}
echo '<table border=1><tr><td>Day</td>';
foreach($daytime as $d)
{echo '<td>'.$d.'</td>';
}
echo '</tr><tr><td>Dates</td>';
foreach($dates as $d)
{echo '<td>'.$d.'</td>';
}
echo '</tr></table>'
?>

希望这对您有用。