Jump to content

Archived

This topic is now archived and is closed to further replies.

mayo

Conflict with MYSQL_FETCH_ARRAY

Recommended Posts

I need to call information from 2 separate tables in my database, and need to output segments from it on two different places on the page.

This is the code I used first of all;

[code]<?php
mysql_connect("localhost", "username", "password") or die(mysql_error());
mysql_select_db("dbname");

$query = "SELECT * FROM Hotel JOIN Specials WHERE Hotel.HotelID = Specials.HotelID AND Hotel.HotelID = '1'";
$result = mysql_query($query);
$row = mysql_fetch_array( $result );
$totalrows = mysql_num_rows($result);
?>[/code]
Near the top of the page, I want to call some information from the 'Hotel' table;

[code]<?php
echo $row['HotelName'];
echo ", ".$row['Town'];
?>[/code]

Further down the page, I want to list all the specials which that hotel has, so I insert the following;

[code]<?php
if ($totalrows < '1') {
?>
<p class="bodytext">No specials available</p>
<?
}
while ($row = mysql_fetch_array($result))
{
?>
<table width="100%"  border="0" cellspacing="0" cellpadding="0">
<tr>
<td class="specialsHeading"><? echo ($row['SpecialTitle']); ?></td>
</tr>
<tr>
<td bgcolor="#255F9B"><img src="../../images/px1.gif" width="1" height="1" /></td>
<tr>
<td><table width="100%"  border="0" cellspacing="0" cellpadding="0">
<tr>
<td><p class="specialBody"><? echo ($row['SpecialDesc']); ?></p></td>
</tr>
<tr>
<td><p class="specialFooter"><? echo ($row</tr>
</table></td>
</tr>
</table>
<?
}
?>[/code]
The problem is that this code outputs the Hotel's Name and Town in the first segment, but misses out the first record in the second loop segment.

After reading a number of forums, I figured out that the initial MYSQL_FETCH_ARRAY uses the first record, and only the subsequent records are outputed in the loop.  The solution to this is to delete the $row = mysql_fetch_array( $result ); statement at the beginning.  It works, and the first record shows up in the loop - but the Hotel Name and Town disappears.

I apologise, but I'm a bit of a noob at PHP and MYSQL, and still trying to learn, but is there something glarringly obvious that I am missing or doing wrong?

I've tried several possible scenarios, but am starting to tear my hair out.  Anyone's help would be greatly appreaciated.

Share this post


Link to post
Share on other sites
With a quick look, you are fetching an array from the result twice. First after the query and then in the while loop.

Another question is, why would you need to loop anything; you are clearly fetching only one result  ;)

Share this post


Link to post
Share on other sites
The final part needs to loop as each hotel often has more than one special at any time.  My question is how best to fetch the information so that each part can access it correctly

Share this post


Link to post
Share on other sites
I'm no expert in sql, so I would do it in separate queries. That is, first get the hotel, then query the specials and loop trought them.

Share this post


Link to post
Share on other sites
Just build your own object from the first pass, and then don't both with the recordset for the second pass, just iterate though your custom structure.

[size=9pt]LazyJones! Long time no see...[/size]

Share this post


Link to post
Share on other sites

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.