开发者

PHP and tables?

开发者 https://www.devze.com 2023-02-01 20:10 出处:网络
Hello Im new in PHP and have some troubles. I have a mysql table called Services. I want to show some columns of that table in a html table. The problem is that I dont how to pass the entities as col

Hello Im new in PHP and have some troubles. I have a mysql table called Services. I want to show some columns of that table in a html table. The problem is that I dont how to pass the entities as column and the atributes as rows. Pleas开发者_StackOverflowe help me!!


For example, I have a table cars with fields in select. Here is how you do it. Assume $conn is my mysql connection.

<?php
echo '<table>';
echo '<tr>';
echo '<th>id</th>';
echo '<th>make</th>';
echo '<th>model</th>';
echo '<th>vin</th>';
echo '</tr>';
$result = mysql_query($conn, 'SELECT id, make, model, vin FROM cars');
while (($row = mysql_fetch_array($result, MYSQLI_ASSOC)) != NULL) {
    echo '<tr>';
    echo '<td>' . $row['id'] . '</td>';
    echo '<td>' . $row['make'] . '</td>';
    echo '<td>' . $row['model'] . '</td>';
    echo '<td>' . $row['vin'] . '</td>';
    echo '</tr>';
}
mysql_free_result($result);
echo '</table>';
?>


You were close. I made a few changes. Does this work as expected?

<?php
echo '<table>';
echo '<tr>';
echo '<th>id</th>';
echo '<th>make</th>';
echo '<th>model</th>';
echo '<th>vin</th>';
echo '</tr>';
$result = mysqli_query($conn, 'SELECT id, make, model, vin FROM cars');
while ($row = mysqli_fetch_assoc($result)) {
    echo '<tr>';
    echo '<td>' . $row['id'] . '</td>';
    echo '<td>' . $row['make'] . '</td>';
    echo '<td>' . $row['model'] . '</td>';
    echo '<td>' . $row['vin'] . '</td>';
    echo '</tr>';
}
mysqli_free_result($result);
echo '</table>';
?>
0

精彩评论

暂无评论...
验证码 换一张
取 消