A MySQL/PHP question
Posted: Fri 26 Apr 2013 5:17 pm
I have been driving myself mad trying to work this out....
I have a web page that compares monthly weather stats for several years. A visitor to the page can select the month they want to view. See here http://www.menstonweather.co.uk/monthcompare.php
For most values I am returning, I have worked out the correct MySQL queries and PHP code to display "N/A" if the month hasn't happened yet, i.e. in the current year.
But one particular stat I am trying to return counts the number of days where the minimum temp dropped below zero. Now obviously for some months this will be zero, e.g. July (in the UK), but also, if the month hasn't yet happened (for the current year) the value will also be zero.
This is the query I am using:
$mo is a numerical value representing the user selected month from the drop down box.
And this is the code that I test the returned value with, to output the result:
I thought that if the month for the current year hasn't happened yet, there would be no results returned and therefore the "false" condition would result, but this doesn't seem to work, it simply displays "0".
You will see that the same problem occurs for the "No. of days without rain" variable as well.
What am I doing wrong?
I have a web page that compares monthly weather stats for several years. A visitor to the page can select the month they want to view. See here http://www.menstonweather.co.uk/monthcompare.php
For most values I am returning, I have worked out the correct MySQL queries and PHP code to display "N/A" if the month hasn't happened yet, i.e. in the current year.
But one particular stat I am trying to return counts the number of days where the minimum temp dropped below zero. Now obviously for some months this will be zero, e.g. July (in the UK), but also, if the month hasn't yet happened (for the current year) the value will also be zero.
This is the query I am using:
Code: Select all
$resulttysmbz=mysql_query("SELECT COUNT(MinTemp) as belowzero FROM Dayfile WHERE YEAR(LogDate)=YEAR(NOW()) AND MONTH(LogDate)=$mo AND MinTemp<0") or die('Error: '.mysql_error());$mo is a numerical value representing the user selected month from the drop down box.
And this is the code that I test the returned value with, to output the result:
Code: Select all
<?php if (mysql_num_rows($resulttysmbz) === false) {?>
<td><?php echo "N/A"?></td>
<?php } else {?>
<?php while($row=mysql_fetch_object($resulttysmbz)){?>
<td><?php echo $row->belowzero;?></td>
<?php
}
?>
<?php
}
?>I thought that if the month for the current year hasn't happened yet, there would be no results returned and therefore the "false" condition would result, but this doesn't seem to work, it simply displays "0".
You will see that the same problem occurs for the "No. of days without rain" variable as well.
What am I doing wrong?