mySQL table query - only returning first row?

Codes here !

Moderators: macek, egami, gesf

dmstewart
New php-forum User
New php-forum User
Posts: 1
Joined: Thu Sep 20, 2012 8:43 am

mySQL table query - only returning first row?

Postby dmstewart » Thu Sep 20, 2012 11:43 pm

Hi all,

I'm fairly new to all this and I'm putting together a site that will advise if a certain resource is free. So lets say there's twenty desks in the office, people can log into them when working on them so others can see what hotdesks are available when coming into the office.

I have a database called 'hotdesk, a table called 'booking' which contains colums 'Ref', 'Deskref', 'Booked', 'Name', 'From' and 'To'.

I have a form which updates the table appropriately when people log into these desks, the database is working fine. However I'm now trying to put together a query which will show which desks are free. This is the php query:

Code: Select all

<?php if($row_Booking_System['Ref'] == "1" && $row_Booking_System['Booked'] == "Y") echo ": Not Available"; ?>

<?php if($row_Booking_System['Ref'] == "2" && $row_Booking_System['Booked'] == "Y") echo ": Not Available";

<?php if($row_Booking_System['Ref'] == "3" && $row_Booking_System['Booked'] == "Y") echo ": Not Available";


Etc.

The first query works, however the second doesnt, I've figured out that the data is only being returned for the first row in the table. So "1" can be found but eveyr other row is not returned. I put the following in and can confirm that it only returns the first row.

Code: Select all

echo "HELLO: " . $row_Booking_System['Ref'];


My database connection info is:

Code: Select all

<?php mysql_select_db($database_hotdesk, $hotdesk);
$query_Booking_System = "SELECT * FROM booking";
$Booking_System = mysql_query($query_Booking_System, $hotdesk) or die(mysql_error());
$row_Booking_System = mysql_fetch_assoc($Booking_System);
$totalRows_Booking_System = mysql_num_rows($Booking_System);
?>


Can anyone advise me what I've done wrong and why my query only works for the first row!?

Thank you very much in advance!

NigelRen
php-forum Active User
php-forum Active User
Posts: 450
Joined: Fri Aug 05, 2011 9:53 am

Re: mySQL table query - only returning first row?

Postby NigelRen » Fri Sep 21, 2012 12:33 pm

After calling mysql_query, you should use something like...

Code: Select all

while ($row_Booking_System = mysql_fetch_assoc($Booking_System)) {
// Do something for each row
}


Return to “mySQL & php coding”

Who is online

Users browsing this forum: No registered users and 1 guest