Jump to content

Conflict with MYSQL_FETCH_ARRAY


mayo

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.
Link to comment
https://forums.phpfreaks.com/topic/17651-conflict-with-mysql_fetch_array/
Share on other sites

Archived

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

×
×
  • Create New...

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.