使用PDO从MySQL表中显示多行

时间:2014-06-03 18:12:47

标签: php mysql pdo

我有一个名为“竞赛”的表格,用于保存用户创建的竞赛。 每行有5列:id,标题,描述,说明和用户名。 ID是将在永久链接中使用的比赛ID,标题将显示在比赛页面和用户的“我的比赛”页面上,说明将显示在比赛页面上,用户名将显示在比赛页面上。我用它来确定谁创造了每个比赛。

我无法在他/她的“我的竞赛”页面中显示所有用户的竞赛。它给了我这个错误:

  

警告:第109行的C:\ xampp \ htdocs \ cpcontests \ i \ my-contests.php中的非法字符串偏移'id'   警告:第109行的C:\ xampp \ htdocs \ cpcontests \ i \ my-contests.php中的非法字符串偏移'title'   第110行的C:\ xampp \ htdocs \ cpcontests \ i \ my-contests.php   1   警告:第111行的C:\ xampp \ htdocs \ cpcontests \ i \ my-contests.php中的非法字符串偏移'描述'   1   警告:第109行的C:\ xampp \ htdocs \ cpcontests \ i \ my-contests.php中的非法字符串偏移量'id'   警告:第109行的C:\ xampp \ htdocs \ cpcontests \ i \ my-contests.php中的非法字符串偏移'title'   第110行的C:\ xampp \ htdocs \ cpcontests \ i \ my-contests.php   1   警告:第111行的C:\ xampp \ htdocs \ cpcontests \ i \ my-contests.php中的非法字符串偏移'描述'   1   警告:第109行的C:\ xampp \ htdocs \ cpcontests \ i \ my-contests.php中的非法字符串偏移量'id'   警告:第109行的C:\ xampp \ htdocs \ cpcontests \ i \ my-contests.php中的非法字符串偏移'title'   第110行的C:\ xampp \ htdocs \ cpcontests \ i \ my-contests.php   Ť   警告:第111行的C:\ xampp \ htdocs \ cpcontests \ i \ my-contests.php中的非法字符串偏移'描述'   Ť   警告:第109行的C:\ xampp \ htdocs \ cpcontests \ i \ my-contests.php中的非法字符串偏移量'id'   警告:第109行的C:\ xampp \ htdocs \ cpcontests \ i \ my-contests.php中的非法字符串偏移'title'   第110行的C:\ xampp \ htdocs \ cpcontests \ i \ my-contests.php   Ť   警告:第111行的C:\ xampp \ htdocs \ cpcontests \ i \ my-contests.php中的非法字符串偏移'描述'   Ť   警告:第109行的C:\ xampp \ htdocs \ cpcontests \ i \ my-contests.php中的非法字符串偏移量'id'   警告:第109行的C:\ xampp \ htdocs \ cpcontests \ i \ my-contests.php中的非法字符串偏移'title'   第110行的C:\ xampp \ htdocs \ cpcontests \ i \ my-contests.php   Ť   警告:第111行的C:\ xampp \ htdocs \ cpcontests \ i \ my-contests.php中的非法字符串偏移'描述'   Ť   警告:第109行的C:\ xampp \ htdocs \ cpcontests \ i \ my-contests.php中的非法字符串偏移量'id'   警告:第109行的C:\ xampp \ htdocs \ cpcontests \ i \ my-contests.php中的非法字符串偏移'title'   第110行的C:\ xampp \ htdocs \ cpcontests \ i \ my-contests.php   Ť   警告:第111行的C:\ xampp \ htdocs \ cpcontests \ i \ my-contests.php中的非法字符串偏移'描述'   Ť   警告:第109行的C:\ xampp \ htdocs \ cpcontests \ i \ my-contests.php中的非法字符串偏移量'id'   警告:第109行的C:\ xampp \ htdocs \ cpcontests \ i \ my-contests.php中的非法字符串偏移'title'   第110行的C:\ xampp \ htdocs \ cpcontests \ i \ my-contests.php   d   警告:第111行的C:\ xampp \ htdocs \ cpcontests \ i \ my-contests.php中的非法字符串偏移'描述'   d   警告:第109行的C:\ xampp \ htdocs \ cpcontests \ i \ my-contests.php中的非法字符串偏移量'id'   警告:第109行的C:\ xampp \ htdocs \ cpcontests \ i \ my-contests.php中的非法字符串偏移'title'   第110行的C:\ xampp \ htdocs \ cpcontests \ i \ my-contests.php   d   警告:第111行的C:\ xampp \ htdocs \ cpcontests \ i \ my-contests.php中的非法字符串偏移'描述'   d   警告:第109行的C:\ xampp \ htdocs \ cpcontests \ i \ my-contests.php中的非法字符串偏移量'id'   警告:第109行的C:\ xampp \ htdocs \ cpcontests \ i \ my-contests.php中的非法字符串偏移'title'   第110行的C:\ xampp \ htdocs \ cpcontests \ i \ my-contests.php   C   警告:第111行的C:\ xampp \ htdocs \ cpcontests \ i \ my-contests.php中的非法字符串偏移'描述'   C   警告:第109行的C:\ xampp \ htdocs \ cpcontests \ i \ my-contests.php中的非法字符串偏移量'id'   警告:第109行的C:\ xampp \ htdocs \ cpcontests \ i \ my-contests.php中的非法字符串偏移'title'位于第110行C:\ xampp \ htdocs \ cpcontests \ i \ my-contests.php   C   警告:第111行的C:\ xampp \ htdocs \ cpcontests \ i \ my-contests.php中的非法字符串偏移'描述'   ç

我的代码:

        <?php
    $table = $config['mysql']['contest_table'];
    $query = $db->con->prepare("SELECT * FROM {$table} WHERE username=:username");
    $query->bindValue(':username', $_SESSION['username']);
    if($query->execute()) {
        $rows = $query->fetch();
        if(count($rows) > 0) {
            foreach($rows as $row) {
                echo "<h3><a href='contests.php?id=" . $row['id'] . ">" . $row['title'] . "</a></h3>";
                echo "<p>" . $row['end_date'] . "</p>";
                echo "<p>" . $row['description'] . "</p>";
                echo "<br />";
            }
        } else {
            echo "<p>You haven't created any contests yet. <a href='create.php'>Create one now</a>.</p>";
        }
    }
    ?>

这是我第一次使用PDO - 我刚从mysql_ *函数中跳了出来。

编辑:我更新了我的代码,错误现在消失了。它只显示有关第二次比赛的信息。

<?php $table = $config['mysql']['contest_table']; $query = $db->con->prepare("SELECT * FROM {$table} WHERE username=:username"); $query->bindValue(':username', $_SESSION['username']); if($query->execute()) { $rows = $query->fetchAll(); var_dump($rows); if(count($rows) > 0) { foreach($rows as $row) { echo "<h3><a href='contests.php?id=" . $row['id'] . ">" . $row['title'] . "</a></h3>"; echo "<p>" . $row['end_date'] . "</p>"; echo "<p>" . $row['description'] . "</p>"; echo "<br />"; } } else { echo "<p>You haven't created any contests yet. <a href='create.php'>Create one now</a>.</p>"; } } ?>

2 个答案:

答案 0 :(得分:0)

fetch应该在while循环中使用,比如

while($row = $query->fetch(PDO::FETCH_ASSOC)) {
 //..
}

如果您想使用foreach循环,请使用fetchAll()

$rows = $query->fetchAll(PDO::FETCH_ASSOC);
foreach($rows as $row) {
//...
}

那是因为fetch()当时获得了一条记录,而fetchAll()一次获得了所有记录。 因此,使用fetch()时,游标需要在循环的每次迭代中移动一行。

答案 1 :(得分:-1)

在你的foreach中使用$rows['id']等,这将帮助你摆脱“警告:非法字符串偏移'id'在”警告