yandoo Posted July 7, 2009 Share Posted July 7, 2009 Hi I was hoping for a little assistance with my current php problem.. Im using a DATEDIFF query to find the number of days there are between the current date and a date stored in my database. This part is all working fine The problem is when i try and add an if statement to the query. I am trying to make it so that if the days are <= 0 then to not output the data for that particular record.. $sql = "SELECT DATEDIFF (ThinDate, CURDATE()) AS intval FROM `vegeschedule` "; $result = mysql_query($sql); if (!$result) { echo "Could not successfully run query ($sql) from DB: " . mysql_error(); exit; } if (mysql_num_rows($result) == 0) { echo "No rows found, nothing to print so am exiting"; exit; } while ($row = mysql_fetch_assoc($result)) { echo $row["intval"]."<br />\n"; } Ive tried to add if ($result <= 0) { echo "N/A"; } But the results i get seem strange as they seem to output the N/A and also the result for that record too... I just need it to NOT output the record if the number of days are <= 0. Im obvoously missing something here .. Any help would be ace Thank You Quote Link to comment https://forums.phpfreaks.com/topic/165096-solved-if-statement-0/ Share on other sites More sharing options...
AwptiK Posted July 7, 2009 Share Posted July 7, 2009 You could just put exit; in that if() statement too. $sql = "SELECT DATEDIFF (ThinDate, CURDATE()) AS intval FROM `vegeschedule` "; $result = mysql_query($sql); if (!$result) { echo "Could not successfully run query ($sql) from DB: " . mysql_error(); exit; } if (mysql_num_rows($result) == 0) { echo "No rows found, nothing to print so am exiting"; exit; } if ($result <= 0) { echo "N/A"; exit; } while ($row = mysql_fetch_assoc($result)) { echo $row["intval"]."<br />\n"; } Without the exit; it will run the while() loop. Quote Link to comment https://forums.phpfreaks.com/topic/165096-solved-if-statement-0/#findComment-870559 Share on other sites More sharing options...
Stryves Posted July 7, 2009 Share Posted July 7, 2009 If Else? $sql = "SELECT DATEDIFF (ThinDate, CURDATE()) AS intval FROM `vegeschedule` "; $result = mysql_query($sql); if (!$result) { echo "Could not successfully run query ($sql) from DB: " . mysql_error(); } else if (mysql_num_rows($result) == 0) { echo "No rows found, nothing to print so am exiting"; } else if ($result <= 0) { echo "N/A"; } else { while ($row = mysql_fetch_assoc($result)) { echo $row["intval"]."<br />\n"; } } Quote Link to comment https://forums.phpfreaks.com/topic/165096-solved-if-statement-0/#findComment-870564 Share on other sites More sharing options...
yandoo Posted July 7, 2009 Author Share Posted July 7, 2009 Hi thanks for the reply, i have 3 records that are outputted and only 1 record has <= 0 days left and wouldnt that mean that the other 2 records wouldnt be displayed also??? Only need the record with <= 0 days to not output the other 2 must display as normal. Thanks Quote Link to comment https://forums.phpfreaks.com/topic/165096-solved-if-statement-0/#findComment-870570 Share on other sites More sharing options...
PFMaBiSmAd Posted July 7, 2009 Share Posted July 7, 2009 $result isn't the value returned by the query. It is a result resource that contains any rows retrieved by the query. One of the great points of using a database is to retrieve just the rows you want in the order you want them. If you are trying to retrieve only the rows WHERE the datediff() expression is greater than zero, use that as a condition in your query - $sql = "SELECT * FROM `vegeschedule` WHERE DATEDIFF(ThinDate, CURDATE()) > 0"; Quote Link to comment https://forums.phpfreaks.com/topic/165096-solved-if-statement-0/#findComment-870574 Share on other sites More sharing options...
sasa Posted July 7, 2009 Share Posted July 7, 2009 <?php $sql = "SELECT DATEDIFF (ThinDate, CURDATE()) AS intval FROM `vegeschedule` "; $result = mysql_query($sql); if (!$result) { echo "Could not successfully run query ($sql) from DB: " . mysql_error(); exit; } if (mysql_num_rows($result) == 0) { echo "No rows found, nothing to print so am exiting"; exit; } while ($row = mysql_fetch_assoc($result)) { if ($row["intval"] <= 0) echo 'N/A'; else echo $row["intval"]; echo "<br />\n"; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/165096-solved-if-statement-0/#findComment-870579 Share on other sites More sharing options...
yandoo Posted July 7, 2009 Author Share Posted July 7, 2009 Hi Thanks for reply, i managed to get it working and found sasa's code worked a charm! Thank you eveyone Quote Link to comment https://forums.phpfreaks.com/topic/165096-solved-if-statement-0/#findComment-870663 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.