Jump to content


Photo

Conflict with MYSQL_FETCH_ARRAY


  • Please log in to reply
4 replies to this topic

#1 mayo

mayo
  • New Members
  • Pip
  • Newbie
  • 2 posts

Posted 15 August 2006 - 05:46 PM

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;

<?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);
?>
Near the top of the page, I want to call some information from the 'Hotel' table;

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

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

<?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>
<?
}
?>
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.

#2 LazyJones

LazyJones
  • Members
  • PipPipPip
  • Advanced Member
  • 78 posts

Posted 15 August 2006 - 06:13 PM

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  ;)

#3 mayo

mayo
  • New Members
  • Pip
  • Newbie
  • 2 posts

Posted 15 August 2006 - 06:17 PM

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

#4 LazyJones

LazyJones
  • Members
  • PipPipPip
  • Advanced Member
  • 78 posts

Posted 15 August 2006 - 06:23 PM

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.

#5 fenway

fenway
  • Staff Alumni
  • MySQL Si-Fu / PHP Resident Alien
  • 16,199 posts
  • LocationToronto, ON

Posted 15 August 2006 - 08:24 PM

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.

LazyJones! Long time no see...
Seriously... if people don't start reading this before posting, I'm going to consider not answering at all.




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users