mayo Posted August 15, 2006 Share Posted August 15, 2006 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]<?phpmysql_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]<?phpecho $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]<?phpif ($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. Quote Link to comment Share on other sites More sharing options...
LazyJones Posted August 15, 2006 Share Posted August 15, 2006 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 ;) Quote Link to comment Share on other sites More sharing options...
mayo Posted August 15, 2006 Author Share Posted August 15, 2006 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 Quote Link to comment Share on other sites More sharing options...
LazyJones Posted August 15, 2006 Share Posted August 15, 2006 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. Quote Link to comment Share on other sites More sharing options...
fenway Posted August 15, 2006 Share Posted August 15, 2006 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] Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.