FlipperBizkut
[H]ard|Gawd
- Joined
- Sep 25, 2002
- Messages
- 1,268
OK... so I have 2 rows of tables that I am trying to populate (one on top of the other). I can get all the information I need through one query of the database, but I need two 'while' statements to populate the web page tables. I am getting no data to the second 'while' statement unless I run the query again. It is easier to explain with the code:
Doing it this way, either the second 'while' statement is not executing, or it is getting no data passed to it. Now, if I put in the same query again (this time before the second 'while' statement), it works exactly as I would want it to. Like this:
Why is it doing this? Is the second example bad programming? Is there anything I can do to make this more 'elegant'? Won't making 2 queries use more resources and take more time than just doing it once?
Thanks for looking.
Code:
$query = "SELECT a, b FROM db";
$result = mysql_query($query);
echo '<tr>';
while ($col = mysql_fetch_array($result, MYSQL_ASSOC)) {
echo '<td>' . $col['a'] . '</td>';
}
echo '</tr><tr>';
while ($col = mysql_fetch_array($result, MYSQL_ASSOC)) {
echo '<td>' . $col['b'] . '</td>';
}
echo '</tr>';
Doing it this way, either the second 'while' statement is not executing, or it is getting no data passed to it. Now, if I put in the same query again (this time before the second 'while' statement), it works exactly as I would want it to. Like this:
Code:
$query = "SELECT a, b FROM db";
$result = mysql_query($query);
echo '<tr>';
while ($col = mysql_fetch_array($result, MYSQL_ASSOC)) {
echo '<td>' . $col['a'] . '</td>';
}
echo '</tr><tr>';
[COLOR=Magenta]$query = "SELECT a, b FROM db";
$result = mysql_query($query);[/COLOR]
while ($col = mysql_fetch_array($result, MYSQL_ASSOC)) {
echo '<td>' . $col['b'] . '</td>';
}
echo '</tr>';
Why is it doing this? Is the second example bad programming? Is there anything I can do to make this more 'elegant'? Won't making 2 queries use more resources and take more time than just doing it once?
Thanks for looking.