Page 1 of 1

while()

Posted: Thu Dec 03, 2009 2:53 pm
by sheppardzwc
Here I am again. :roll:

I'm trying to while() loop 20 rows of data in MySQL. I want it to be displayed in a table using the while() loop, yet when I use it, it just repeats the same line over and over again. How can I fix this? (the table is already declared)

Code: Select all

    echo('<br />
        <tr><th>Author</th><th>Quote</th></tr>');
    while($quote_list_result = $db->fetch_array($db->query_read("SELECT * FROM quote ORDER BY id ASC"))) {
        echo('<tr>');
        echo('<td>' . $quote_list_result['author'] . '</td>');
        echo('<td>' . $quote_list_result['text'] . '</td>');
        echo('</tr>');
    }

Re: while()

Posted: Thu Dec 03, 2009 3:13 pm
by AbraCadaver
Well, because you are running a new query each time through the loop so you always get the first record. 8O Just query once and then fetch a new row from the query result each time through the loop:

Code: Select all

$result = $db->query_read("SELECT * FROM quote ORDER BY id ASC");
 
while($quote_list_result = $db->fetch_array($result)) {
        echo('<tr>');
        echo('<td>' . $quote_list_result['author'] . '</td>');
        echo('<td>' . $quote_list_result['text'] . '</td>');
        echo('</tr>');
}
-Shawn