Php 将结果包装在表中

Php 将结果包装在表中,php,Php,我有一个php代码,它从数据库中回显结果。 代码如下: <?php $db = new mysqli("localhost", "root", "", "garyline_master"); if(isset($_GET['keywords'])) { $keywords = $db->escape_string($_GET['keywords']); $query = $db->query(" SEL

我有一个php代码,它从数据库中回显结果。 代码如下:

<?php

   $db = new mysqli("localhost", "root", "", "garyline_master");

   if(isset($_GET['keywords'])) {

        $keywords = $db->escape_string($_GET['keywords']);

        $query = $db->query("
            SELECT productID, productName, description
            FROM promo
            WHERE productID LIKE '%{$keywords}%'
            OR productID LIKE '%{$keywords}%'
        ");
   ?>

   <div class="resul-count">
    Found <?php echo $query->num_rows; ?> Results.
    </div>

    <table border="1" align="center">   
    <?php

        if($query->num_rows){
            while($r = $query->fetch_object()){
            ?>
            <div class="result">
                    <tr><?php 
                    echo $r->productID;
                    echo $r->productName;
                    echo $r->description;
            ?>
            </tr>
                </div>
    </table>
            <?php
        }

}
}   
?>

查看您的代码,您在此处回显结果的部分:

while($r = $query->fetch_object()){
        ?>
        <div class="result">
                <tr><?php
                echo $r->productID;
                echo $r->productName;
                echo $r->description;
        ?>
        </tr>
while($r=$query->fetch\u object()){
?>
首先,我觉得类为“result”的div很奇怪。为什么不简单地将该类应用于tr?其次,您没有“td”元素。也许这就是您需要的:

while($r = $query->fetch_object()){
    echo '<tr class="result">';
    echo "<td>$r->productID</td>";
    echo "<td>$r->productName</td>";
    echo "<td>$r->description</td>";
    echo '</tr>';
}
while($r=$query->fetch\u object()){
回声';
回显“$r->productID”;
回显“$r->productName”;
回显“$r->说明”;
回声';
}

正确地将查询结果与表元素组合在一起,应该会得到您想要的结果

<tr>
    <td><?php echo $r->productID;   ?></td>
    <td><?php echo $r->productName; ?></td>
    <td><?php echo $r->description; ?></td>
</tr>

您的代码在html表示的表结构中有一些错误

  • 不要在表中使用“div”(可以在td中使用,请参见下一主题)
  • 您的数据必须放在一个“td”标记中,该标记可以包含html
html中的干净表格如下所示:

表格{
字体系列:sans;
}
th,
运输署{
背景色:#ddd;
}

冷头
冷头
冷头
1/1
1/2
1/3
2/1
2/2
2/3
3/1
3/2
3/3

那么错误是什么呢?问题中应该是代码,而不是外部链接。请选择。