By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
454,404 Members | 1,705 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 454,404 IT Pros & Developers. It's quick & easy.

missing array element

P: n/a
Hi,

I was wondering if anyone could help me with a problem I am having with
an array. I am only a beginner, so please be gentle!

I am trying to store the id's for 3 hotel rooms into an array called
$rooms, using a simple mysql select statement. Here is the code:
<?php require_once('Connections/Vita_Italiano.php'); ?>
<?php mysql_select_db($database_Vita_Italiano, $Vita_Italiano);
$query_rsAllRooms = "SELECT * FROM room";

$rsAllRooms = mysql_query($query_rsAllRooms, $Vita_Italiano) or
die(mysql_error());

$row_rsAllRooms = mysql_fetch_assoc($rsAllRooms);
$totalRows_rsAllRooms = mysql_num_rows($rsAllRooms);

// create array of all rooms
while ($row_rsAllRooms = mysql_fetch_assoc($rsAllRooms))
{
$rooms[]= $row_rsAllRooms['Room_Number'];
}

?>

When I use <?php print_r($rooms);?> to display the contents of the
variable, the array only contains 2 room numbers, instead of 3. These
are stored in $rooms[0] and $rooms[1].

Does anyone have any ideas what is going on here?

Thanks in advance,

David Robinson

Jul 17 '05 #1
Share this Question
Share on Google+
4 Replies


P: n/a
On 29 May 2005 11:19:50 -0700, da**************@sunderland.ac.uk wrote:
$row_rsAllRooms = mysql_fetch_assoc($rsAllRooms);
while ($row_rsAllRooms = mysql_fetch_assoc($rsAllRooms))

Does anyone have any ideas what is going on here?


Yes: you throw away the first row in this first statement.
--
Firefox Web Browser - Rediscover the web - http://getffox.com/
Thunderbird E-mail and Newsgroups - http://gettbird.com/
Jul 17 '05 #2

P: n/a
Ewoud Dronkert wrote:
On 29 May 2005 11:19:50 -0700, da**************@sunderland.ac.uk wrote:
$row_rsAllRooms = mysql_fetch_assoc($rsAllRooms);
while ($row_rsAllRooms = mysql_fetch_assoc($rsAllRooms))

Does anyone have any ideas what is going on here?


Yes: you throw away the first row in this first statement.


Thanks for the quick reply Ewoud. I've taken the
$row_rsAllRooms = mysql_fetch_assoc($rsAllRooms)
line out and it works now. I don't really understand why taking that
line out solves the problem, but the main thing is that it works.

Thanks for your time.

David Robinson

Jul 17 '05 #3

P: n/a
On 29 May 2005 13:41:38 -0700, da**************@sunderland.ac.uk wrote:
I don't really understand why taking that line out solves the problem


It's because there are three rows in the result set, and you retrieved
the first one with that line. The "result set pointer" moves one row
ahead with that action, and consequently in the while loop you only have
two rows left. Count how many times you called mysql_fetch_assoc(): once
in the line now removed and twice in the while loop.
--
Firefox Web Browser - Rediscover the web - http://getffox.com/
Thunderbird E-mail and Newsgroups - http://gettbird.com/
Jul 17 '05 #4

P: n/a
Thanks very much the explanation. That's really helpful to know.

Cheers

David

Jul 17 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.