Page 1 of 1

Mysql_fetch_assoc - Looping thru result sets

Posted: Mon Aug 19, 2002 9:15 pm
by markwenham
Can anyone tell me why this doesn't display two tables? The first table and
result set shows up, but the secong table does not appear. I have tried
resetting $allhotels but that gives me an error.

---Code----
<?
$allhotels_query = "select ID, hot_cod from hotelbkg order by hot_cod";
$allhotels = mysql_query($allhotels_query) or
die (mysql_error());

?>
<form method="post" action="<?$PHP_SELF?>">
<table border="1">
<?
while ($row = mysql_fetch_assoc($allhotels)){
?>
<tr>
<td><input name="bkg_ref" value="<?echo $row["hot_cod"]?>"></td>
<td><input name="ID" value="<?echo $row["ID"]?>"></td>
</tr>
<?}?>
</table>

<table border="1">
<?
reset($allhotels);
while ($row = mysql_fetch_assoc($allhotels)){
?>
<tr>
<td><input name="bkg_ref" value="<?echo $row["hot_cod"]?>"></td>
<td><input name="ID" value="<?echo $row["ID"]?>"></td>
</tr>
<?}?>
</table>
</form>
---Finish Code---

Thanks, Mark

Posted: Mon Aug 19, 2002 9:17 pm
by markwenham
Sorry forgot to use the code highlighter

Can anyone tell me why this doesn't display two tables? The first table and
result set shows up, but the second table does not appear. I have tried
resetting $allhotels but that gives me an error.

Code: Select all

<?
$allhotels_query = "select ID, hot_cod from hotelbkg order by hot_cod";
$allhotels = mysql_query($allhotels_query) or
     die (mysql_error());

?>
<form method="post" action="<?$PHP_SELF?>">
<table border="1">
<?
while ($row = mysql_fetch_assoc($allhotels))&#123;
?>
<tr>
 <td><input name="bkg_ref" value="<?echo $row&#1111;"hot_cod"]?>"></td>
 <td><input name="ID" value="<?echo $row&#1111;"ID"]?>"></td>
</tr>
<?&#125;?>
</table>

<table border="1">
<?
reset($allhotels);
while ($row = mysql_fetch_assoc($allhotels))&#123;
?>
<tr>
 <td><input name="bkg_ref" value="<?echo $row&#1111;"hot_cod"]?>"></td>
 <td><input name="ID" value="<?echo $row&#1111;"ID"]?>"></td>
</tr>
<?&#125;?>
</table>
</form>
Thanks, Mark

Posted: Mon Aug 19, 2002 9:38 pm
by volka
$allhotes is not an array but a resource identifier.
If you want to get the data twice request it again from mysql or store every line in an extra array.

Posted: Tue Aug 20, 2002 5:23 am
by mikeq
volka wrote:$allhotes is not an array but a resource identifier.
If you want to get the data twice request it again from mysql or store every line in an extra array.
No need to do this, just use the MySQL function equivalent to reset()

mysql_data_seek($allhotels,0);

This will put the internal pointer to the start of your result set.

Posted: Tue Aug 20, 2002 5:34 am
by markwenham
Excellent Mikeq - Thank You! :lol:

good job!

Posted: Wed Oct 23, 2002 6:17 am
by lanlord
mikeq wrote:No need to do this, just use the MySQL function equivalent to reset()

mysql_data_seek($allhotels,0);

This will put the internal pointer to the start of your result set.

A total ass-saver. Thanks brah! :D

Posted: Wed Oct 23, 2002 7:13 am
by volka
always something new to learn :D

That will save a post

Posted: Wed Oct 23, 2002 12:49 pm
by phpScott
That little titbit will save me making an extra post.
Wait I just did? :oops:

Posted: Thu Oct 24, 2002 5:25 am
by Takuma
Sorry forgot to use the code highlighter
how about using the highlighter for PHP?

Code: Select all

&lt;?php
//BlahBalh

echo hello;
?&gt;