PHP SQL只返回一个结果

时间:2016-08-27 10:08:34

标签: php sql pdo wamp

我试图将我的网站从我的本地wamp项目迁移到我的实时服务器进行测试,一切都在我的本地网站上正常工作,但在我的实时网站上,以下代码只返回一个结果。肯定有多个SQL条目符合sql查询条件。有什么建议吗?

$opentickets = $db->query("SELECT tID, id, date, userid, category, department FROM ticket WHERE userid = '$_SESSION[id]' AND status='Open'");

if(count($opentickets) > 0) {
echo "<h2>Your open tickets:</h2>";
echo "<table class='table table-striped'>
    <tr>
    <th>Ticket</th>
    <th>Date Submitted</th>
    <th>Category</th>
    <th>Department</th></tr>";
$o = $opentickets->fetch(PDO::FETCH_ASSOC);
echo "<tr><td><a href='ticket.php?id=" . $o['tID'] . "'>" . $o['tID'] . </td>";
echo "<td>" . $o['date'] . "</td>";
echo "<td>" . $o['category'] . "</td>";
echo "<td>" . $o['department'] . "</td></tr>";
echo "</table>";
 }
else
{
echo "<h2>You have no open tickets</h2>";
}

1 个答案:

答案 0 :(得分:1)

您没有考虑查询结果,只是获取第一行......

请参阅PHP Website上有关如何执行此操作的示例。

我刚刚更改了您的代码,请查看手册以获取更多信息

while($o = $opentickets->fetch(PDO::FETCH_ASSOC)){
        echo "<tr><td><a href='ticket.php?id=" . $o['tID'] . "'>" .$o['tID'] . </td>";
        echo "<td>" . $o['date'] . "</td>";
        echo "<td>" . $o['category'] . "</td>";
        echo "<td>" . $o['department'] . "</td></tr>";
}