Mysql_fetch_assoc - Looping thru result sets

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
markwenham
Forum Newbie
Posts: 13
Joined: Fri Aug 16, 2002 11:33 pm

Mysql_fetch_assoc - Looping thru result sets

Post 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
markwenham
Forum Newbie
Posts: 13
Joined: Fri Aug 16, 2002 11:33 pm

Post 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
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post 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.
User avatar
mikeq
Forum Regular
Posts: 512
Joined: Fri May 03, 2002 3:33 am
Location: Edinburgh, Scotland

Post 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.
markwenham
Forum Newbie
Posts: 13
Joined: Fri Aug 16, 2002 11:33 pm

Post by markwenham »

Excellent Mikeq - Thank You! :lol:
User avatar
lanlord
Forum Newbie
Posts: 13
Joined: Wed Sep 18, 2002 10:02 am

good job!

Post 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
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

always something new to learn :D
User avatar
phpScott
DevNet Resident
Posts: 1206
Joined: Wed Oct 09, 2002 6:51 pm
Location: Keele, U.K.

That will save a post

Post by phpScott »

That little titbit will save me making an extra post.
Wait I just did? :oops:
User avatar
Takuma
Forum Regular
Posts: 931
Joined: Sun Aug 04, 2002 10:24 am
Location: UK
Contact:

Post 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;
Post Reply