我真的搞不懂我在这里做错了什么。我正在执行一个查询,以检查名为“newCards”的DB表中是否有记录。
使用$count我检查返回的结果:它显示“1”。但是while循环没有返回任何东西。我看到的只是表顶部的<th>,但是没有表记录,而$count结果是给出了'1'。这是真的,因为数据库中实际上有一条记录。
我该怎么解决?

<?php
    $query = $db->prepare("SELECT * FROM `newCards` WHERE `company` = :companyID");
    $query->bindParam(":companyID", $enterprise['id'], PDO::PARAM_INT);
    $query->execute();
    $count = $query->rowCount();
    echo $count;

    if(empty($query->fetch())){
        echo "Geen gevonden";
    } else {
?>
                    <table>
                        <tr>
                            <th>Ontvanger</th>
                            <th>Saldo</th>
                            <th></th>
                        </tr>
<?php
        while($result = $query->fetch()){
?>
                        <tr>
                            <td><?php echo $result['id']; ?></td>
                            <td>2</td>
                            <td>3</td>
                        </tr>
<?php
        }
?>
                    </table>
<?php
    }
?>

最佳答案

$query->fetch()已获取记录。所以下一个调用fetch()将获取下一个记录,如果没有记录,则不获取任何记录。在您的情况下,一秒的记录不会获取任何信息,因此从不启动。
您可以将代码更改为:

if($count){?>
<table>
    <tr>
        <th>Ontvanger</th>
        <th>Saldo</th>
        <th></th>
    </tr>
<?php
    while($result = $query->fetch()){
        // show row
    }?>
</table>
} else {
    // echo that no rows found
}

09-10 00:11
查看更多