View Full Version : error in loop perhaps?

03-01-2004, 12:49 PM
Here is some code that I am having alot of trouble with, and i can't seem to
figure what what i am doing wrong.

i want to print out a table. a cell should display 'B' if the query in the database returns a result
showing that there is a match. Or a cell should display 'A' if the query returns an empty set.

At the moment, all the cells show 'A' even though there are entries in the database that match.

I suspect an error in the loop somewhere, but i don't know where.

Please can anyone help??


while ($new_fday <= $tday)

$make_date = date ("j/n/Y", mktime(0,0,0,$fmonth,$new_fday,$fyear));
echo "<tr><td><b>$make_date</b></td>";
while($fromtimex < $totime)

$conn = mysql_connect();
mysql_select_db("test", $conn);

$sql = "SELECT bookingid FROM booking WHERE roomnum = '$roomsel' and
startdate = '$make_date' and (('$fromtimey' between starttime and endtime)
or starttime = '$fromtimey')";

$result = mysql_query($sql, $conn) or die(mysql_error());

$num = mysql_num_rows($result); // Count number of matching rows

echo "<td>B</td>";

echo "<td>A</td>";


/*echo "<td>&nbsp</td>";*/
$fromtimex = $fromtimex +1;

$new_fday = $new_fday +1;
echo "</tr>";
$fromtimex = $fromtimey;
}/*end while*/
echo "</table>";

03-01-2004, 12:57 PM
But the query itself is ok and returns the expected result set? In other words, print out the query, and let it run in phpMyAdmin, and see what kind of results are returned. Apart from that I don't see any syntax errors in the loop.

03-01-2004, 04:39 PM
i haven't used phpMyAdmin but i have tried the query in mysql
and i did get the result i expected.

Is there anything else that could be wrong as to why i dont get the correct display output?

03-01-2004, 05:46 PM
Hmh, you could check if the variables $make_date etc. all contain correct values by tracking them during execution of the loop. You could also use var_dump($result) to show you if the selected records actually contain what you expect.

If that doesn't help, you need to show us the structure of your db tables, and some sample data we can use to test your code on our own servers.

03-01-2004, 06:08 PM
thanks for your help, i'll give it a try and i'll get back to you with what happens.