Hi, im new to php and have just learned the basics of
reading / writing to a mysql database using a php web
front end... I have this code (below)
All it is doing is populating a text field (for my debugging purposes) with the number of records within those specified dates...
In any case my problem is with the if statement for the count(*) function.
It will simply not work, it will say it is not greater than zero, when infact it is, or, when i put quotations around the '0' the opposite occurs...

Ive been trying to figure this out for hours...
help would be soooooo much appreciated

<?php
mysql_connect("localhost", "root", "password");
$result = mysql_query("select count()
from hotel.bookings
Where RoomID = '1' and DateCheckin <= '06/01/2005' and DateCheckout >= '06/03/2005'") or
die (mysql_error());
while ($row = mysql_fetch_array($result))
{
echo "<input name='result' value=" . $row["count(
)"] . ">";
if ( "count(*)" > 0 ) {
echo "greater than zero";
} else {
echo "less than zero";
}
}
?>

    Hi,

    First of all, in the FROM clause you have 'hotel.bookings'. Is that correct ... database called 'hotel', table called 'bookings'?

    Secondly, is the field 'RoomID' numeric or char-based (VARCHAR or whatever). If it's numeric, get rid of the single-quotes around the 1 in the WHERE clause.

    Anyway, give the calculated field an alias (using the AS keyword). Then refer to that in the PHP ...

    <?php 
    mysql_connect("localhost", "root", "password"); 
    
    $result = mysql_query("
    	SELECT COUNT(*) AS `bookings_count`
    	FROM hotel.bookings 
    	WHERE RoomID = '1' 
    	AND DateCheckin <= '06/01/2005' 
    	AND DateCheckout >= '06/03/2005'
    ") or die (mysql_error()); 
    
    while ($row = mysql_fetch_array($result)){ 
    	echo '<input name="result" value="'.$row['bookings_count'].'">'; 
    	if(0 < $row['bookings_count']){ 
    		echo "greater than zero"; 
    	} else { 
    		echo "less than (or equal to) zero"; 
    	} 
    } 
    ?>

    Paul 🙂

    PS. I couldn't resist tweaking your code ...

    Use single-quoted strings rather than double-quoted, especially for HTML (loads of books, tutorials use double-quoted strings, but they are often just lazy in their explanation of why.)

    Spread your code out at bit ... especially when you are learning ... just makes life easier when it comes to debugging etc.

      i cant thank you enough, i tried it and it works!!! finally!!!
      The select statement and count(*) functions were working
      as i was receiving output of the number of records found
      in the text field, but something was up with my if statement.

      Anyway my code is now much neater and I have understood
      your amendments, thank you very much

      Peter

        Write a Reply...