Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: PHP MySQL
hi,
I am facing an unusual problem when comparing two dates.Pleas have a look.
Database table
 
Warranty_renewal_date (Date type= DATE)
2010-02-12
2010-02-12
2013-06-11
 
My code in php
 
$sql4="select count(*) as Under_warrenty from machine_details where warranty_renewal_date >=".$current_date;
$result4 = mysql_query($sql4) or die('Query failed: ' . mysql_error());
if ($result4) {
  while ($row4 = mysql_fetch_assoc($result4)) {
      $totlapoll4=$row4["Under_warrenty"];
      
	    }
}
 
 
$sql5="select count(*) as OutOf_warrenty from machine_details where warranty_renewal_date <=".$current_date ;
$result5 = mysql_query($sql5) or die('Query failed: ' . mysql_error());
if ($result5) {
  while ($row5 = mysql_fetch_assoc($result5)) {
      $totlapoll5=$row5["OutOf_warrenty"];
      
	    }
}
 
 
 
In this case what i should get
$totlapoll4=1
$totlapoll5=2
But i am getting 3 and 0.
Can anyone suggest whats wrong here.?
Thanks
Posted 7-May-13 21:16pm
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

echo sql query and check it by execute that in MySQL
 
Check Date format passed from PHP it should be yyyy-MM-dd
 
and in first query you are using >= then in second query you should use < only instead of <= it's just for correction.
 
Happy Coding!
Smile | :)
  Permalink  
v3
Comments
Rajeshkrathor at 8-May-13 2:31am
   
hi Aarti,
Thanks for your reply but i have done all getting same ans in both Mysql and in PHP.
that's why i post here to find out the bug.
Aarti Meswania at 8-May-13 2:38am
   
check updated solution
Rajeshkrathor at 8-May-13 2:42am
   
hey, That was not reason. its just a matter of '' quote.
Aarti Meswania at 8-May-13 2:48am
   
good you find it
and how? by echo/print query and then trying it in mysql - so, it show you error and you correct it :)
Rajeshkrathor at 8-May-13 3:05am
   
return 3 and 0 and Incorrect date value: '2000' for column 'warranty_renewal_date' at row 1 message and by using with '' colon giving the correct ans with no message.
Aarti Meswania at 8-May-13 3:15am
   
exactly that was reason.
And it is simplest way to get exact reaason for error and fix it by testing query in database which was prepared in PHP.
Rajeshkrathor at 8-May-13 3:16am
   
Ok.
thanks
i was just checking same query again and again in both PHP and MYSQL.
Aarti Meswania at 8-May-13 3:23am
   
:)
when ever you face problem like this always check query in database prepared from php code it can give you exact hint so you can fix it easily
Have a nice time! :)
Rajeshkrathor at 8-May-13 3:26am
   
Sure!
Thanks for your advice..
Aarti Meswania at 8-May-13 3:29am
   
Welcome!
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

hi,
This solution code.
 
 

$sql4="select count(*) as Under_warrenty from machine_details where warranty_renewal_date >='$current_date'";
$result4 = mysql_query($sql4) or die('Query failed: ' . mysql_error());
if ($result4) {
  while ($row4 = mysql_fetch_assoc($result4)) {
      $totlapoll4=$row4["Under_warrenty"];
      
	    }
}
 
 
$sql5="select count(*) as OutOf_warrenty from machine_details where warranty_renewal_date <'$current_date'" ;
$result5 = mysql_query($sql5) or die('Query failed: ' . mysql_error());
if ($result5) {
  while ($row5 = mysql_fetch_assoc($result5)) {
      $totlapoll5=$row5["OutOf_warrenty"];
      
	    }
}
 
What a silly thing!
  Permalink  

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 Marcin Kozub 265
1 Richard MacCutchan 239
2 OriginalGriff 208
3 Sergey Alexandrovich Kryukov 185
4 Praneet Nadkar 167
0 OriginalGriff 7,913
1 Sergey Alexandrovich Kryukov 7,232
2 DamithSL 5,604
3 Manas Bhardwaj 4,986
4 Maciej Los 4,865


Advertise | Privacy | Mobile
Web01 | 2.8.1411023.1 | Last Updated 8 May 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100