使用PDO从MySQL表中显示多行

I have a table called "contests" which holds contests created by users. Each row has 5 columns: id, title, description, instructions and username. ID is the id of the contest which will be used in the permalink, title will be shown on the contest page and the user's "my contests" page, instructions will be shown on the contest page and the username will be shown on the contest page. I use it to determine who created each contest.

I'm having trouble displaying all the user's contests in his/her "My Contests" page. It gives me this error:

Warning: Illegal string offset 'id' in C:\xampp\htdocs\cpcontests\i\my-contests.php on line 109 Warning: Illegal string offset 'title' in C:\xampp\htdocs\cpcontests\i\my-contests.php on line 109 C:\xampp\htdocs\cpcontests\i\my-contests.php on line 110 1 Warning: Illegal string offset 'description' in C:\xampp\htdocs\cpcontests\i\my-contests.php on line 111 1 Warning: Illegal string offset 'id' in C:\xampp\htdocs\cpcontests\i\my-contests.php on line 109 Warning: Illegal string offset 'title' in C:\xampp\htdocs\cpcontests\i\my-contests.php on line 109 C:\xampp\htdocs\cpcontests\i\my-contests.php on line 110 1 Warning: Illegal string offset 'description' in C:\xampp\htdocs\cpcontests\i\my-contests.php on line 111 1 Warning: Illegal string offset 'id' in C:\xampp\htdocs\cpcontests\i\my-contests.php on line 109 Warning: Illegal string offset 'title' in C:\xampp\htdocs\cpcontests\i\my-contests.php on line 109 C:\xampp\htdocs\cpcontests\i\my-contests.php on line 110 T Warning: Illegal string offset 'description' in C:\xampp\htdocs\cpcontests\i\my-contests.php on line 111 T Warning: Illegal string offset 'id' in C:\xampp\htdocs\cpcontests\i\my-contests.php on line 109 Warning: Illegal string offset 'title' in C:\xampp\htdocs\cpcontests\i\my-contests.php on line 109 C:\xampp\htdocs\cpcontests\i\my-contests.php on line 110 T Warning: Illegal string offset 'description' in C:\xampp\htdocs\cpcontests\i\my-contests.php on line 111 T Warning: Illegal string offset 'id' in C:\xampp\htdocs\cpcontests\i\my-contests.php on line 109 Warning: Illegal string offset 'title' in C:\xampp\htdocs\cpcontests\i\my-contests.php on line 109 C:\xampp\htdocs\cpcontests\i\my-contests.php on line 110 t Warning: Illegal string offset 'description' in C:\xampp\htdocs\cpcontests\i\my-contests.php on line 111 t Warning: Illegal string offset 'id' in C:\xampp\htdocs\cpcontests\i\my-contests.php on line 109 Warning: Illegal string offset 'title' in C:\xampp\htdocs\cpcontests\i\my-contests.php on line 109 C:\xampp\htdocs\cpcontests\i\my-contests.php on line 110 t Warning: Illegal string offset 'description' in C:\xampp\htdocs\cpcontests\i\my-contests.php on line 111 t Warning: Illegal string offset 'id' in C:\xampp\htdocs\cpcontests\i\my-contests.php on line 109 Warning: Illegal string offset 'title' in C:\xampp\htdocs\cpcontests\i\my-contests.php on line 109 C:\xampp\htdocs\cpcontests\i\my-contests.php on line 110 d Warning: Illegal string offset 'description' in C:\xampp\htdocs\cpcontests\i\my-contests.php on line 111 d Warning: Illegal string offset 'id' in C:\xampp\htdocs\cpcontests\i\my-contests.php on line 109 Warning: Illegal string offset 'title' in C:\xampp\htdocs\cpcontests\i\my-contests.php on line 109 C:\xampp\htdocs\cpcontests\i\my-contests.php on line 110 d Warning: Illegal string offset 'description' in C:\xampp\htdocs\cpcontests\i\my-contests.php on line 111 d Warning: Illegal string offset 'id' in C:\xampp\htdocs\cpcontests\i\my-contests.php on line 109 Warning: Illegal string offset 'title' in C:\xampp\htdocs\cpcontests\i\my-contests.php on line 109 C:\xampp\htdocs\cpcontests\i\my-contests.php on line 110 C Warning: Illegal string offset 'description' in C:\xampp\htdocs\cpcontests\i\my-contests.php on line 111 C Warning: Illegal string offset 'id' in C:\xampp\htdocs\cpcontests\i\my-contests.php on line 109 Warning: Illegal string offset 'title' in C:\xampp\htdocs\cpcontests\i\my-contests.php on line 109 C:\xampp\htdocs\cpcontests\i\my-contests.php on line 110 C Warning: Illegal string offset 'description' in C:\xampp\htdocs\cpcontests\i\my-contests.php on line 111 C

My code:

        <?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>";
        }
    }
    ?>

This is my first time using PDO - I just made the leap from mysql_* functions.

Edit: I updated my code and the errors are now gone. It only displays information about the second contest though.

<?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>"; } } ?>

in your foreach use $rows['id'] etc this wil help you to get rid of the "Warning: Illegal string offset 'id' in" warning

fetch should be use in a while loop, like this

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

if you wanted to use a foreach loop use fetchAll()

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

That's because fetch() get one record at the time, while fetchAll() gets it all at one time. So using fetch() the cursor needs moves a row at each iteration of the loop.