while()

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
sheppardzwc
Forum Newbie
Posts: 20
Joined: Mon Aug 17, 2009 3:04 pm

while()

Post 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>');
    }
User avatar
AbraCadaver
DevNet Master
Posts: 2572
Joined: Mon Feb 24, 2003 10:12 am
Location: The Republic of Texas
Contact:

Re: while()

Post 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
mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.
Post Reply