Click here to Skip to main content
15,886,761 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,

I have a some code that retrieves a row from a database and stores it in a variable called $row.

using a while function, for every record in database that is retrieved using sql, I have to put into a table.

I have got that working but for every new record I get a new table is created instead of the new row being added to the table. For example, the first table displays the first record retrieved (call this A). then the second table displays the first two records retrieved (A from before, and the new one which is B), the third table displays A,B and the new one called C, and so on.

this is the code i have below, I dont know if it is a big error im not seeing or if it is just a small mistake ive not seen:

PHP
$sqlQuery = "MY CORRECT QUERY; ";

$stmt = $db->query( $sqlQuery );

$table = <<<EOD
<table border="1" cellpadding="0" width="100%">
    <tr>
        <th></th>
        <th></th>
        <th></th>
        <th></th>
        <th></th>
    <tr>

EOD;

while ( $s= $stmt->fetchObject()){

   $row = "<tr><td>" . $s->studentCode .
       "</td>\n<td>" . $s->surname .
       "</td>\n<td>" . $s->forename .
       "</td>\n<td>" . $s->typeDesc .
       "</td>\n<td>" . $s->startYear . "</td>\n</tr><br />";

    $table .= $row;

    echo $table;

}
?>

HTML
</table>
Posted

1 solution

As predicted, Ive found that there was a small mistake in my code.

In the while statement at the bottom, I closed the bracket AFTER the line that says:

PHP
echo $table;


This means that, for each row retrieved in the while function, the table is echoed, which explains why there is a new table for each new row!
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900