mysql的输出无法在表

时间:2018-05-21 09:08:52

标签: javascript php mysql

我使用简单的AJAX来显示数据库中的数据。但是,当我运行它时,我的表上只显示了1个数据,其余数据显示在我的表外。我错过了什么吗?

我在我的标准Bootstrap表中使用相同的代码,它正确地将数据显示在我的表中,但不是这个。

我的PHP文件



<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "tata_kota1";

$conn = new mysqli($servername, $username, $password, $dbname);
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
} 

$sql = "SELECT * from laporan_gini";
$result = $conn->query($sql);

echo "<table border='1'>
<thead>
<tr>
<th>Nomor</th>
<th><b>tipe variabel</b></th>
<th><b>variabel turunan</b></th>
<th><b>nama atahun</b></td></th>
<th><b>turunan tahun</b></td></th>
<th><b>data_content</b></td></th>
<th><b>vertical variabel</b></th>
</tr>
</thead>";

if ($result->num_rows > 0) {
    while($row = $result->fetch_assoc()) {
        {
            echo "<tbody><tr>";
            echo "<th>".$row['id']."</th>";
            echo "<td>".$row['nama_variabel']."</td>";
            echo "<td>".$row['nama_variabel_turunan']."</td>";
            echo "<td>".$row['nama_tahun']."</td>";
            echo "<td>".$row['nama_turunan_tahun']."</td>";
            echo "<td>".$row['data_content']."</td>";
            echo "<td>".$row['nama_item_vertical_variabel']."</td>";
            echo "</tr>";
            
        }
        echo "</tbody></table></table>";
    }
} else {
    echo "0 results";
}

?>
&#13;
&#13;
&#13;

2 个答案:

答案 0 :(得分:2)

你需要在while循环之前编写tbody。

答案 1 :(得分:1)

您的问题是{}内部有whileecho "</tbody></table></table>";位于while内,而不在if ($result->num_rows > 0) { echo "<tbody>"; while($row = $result->fetch_assoc()) { echo "<tr>"; echo "<th>".$row['id']."</th>"; echo "<td>".$row['nama_variabel']."</td>"; echo "<td>".$row['nama_variabel_turunan']."</td>"; echo "<td>".$row['nama_tahun']."</td>"; echo "<td>".$row['nama_turunan_tahun']."</td>"; echo "<td>".$row['data_content']."</td>"; echo "<td>".$row['nama_item_vertical_variabel']."</td>"; echo "</tr>"; } echo "</tbody></table></table>"; } else { echo "0 results"; } 之内。试试这个:

for($i=4;$i<count($posts);$i++)
{
    if ($current['deleted'] === true) 
    {
      $i -= 4; // jump back
    }
    $current = $posts[$i];

    $html .= '<div class="content">' . $content . '</div>';
}