Dharmender Posted January 10, 2013 Share Posted January 10, 2013 The Mysql query in PHP gives 0 records where as the query shows 5 records in Mysql. Code in PHP is $fromdate=trim($_POST['fromdate']); $from=date('d-m-Y',strtotime($fromdate)); $todate=trim($_POST['todate']); $to=date('d-m-Y',strtotime($todate)); $supname=$_POST['supplier']; $sql="select a.date,(select itemname from items where itemid=a.itemid) as item,a.quantity,a.rate,a.fat,a.amount from purchasedetails a where a.supplierid=".$supname." and (a.date >='".$from."' and a.date <='".$to."')"; $result=mysql_query($sql); $count=mysql_num_rows($result) echo $count; Query in Mysql select a.date,(select itemname from items where itemid=a.itemid) as item,a.quantity,a.rate,a.fat,a.amount from purchasedetails a where a.supplierid=2 and a.date>='01-12-2012' and a.date<='10-01-2013'; date column in Mysql table is varchar type. Quote Link to comment Share on other sites More sharing options...
haku Posted January 10, 2013 Share Posted January 10, 2013 Since there are only three differences (the three variables) between your PHP code and the code you are typing directly into mysql, it stands to reason that there is something different in the three variables than what you are expecting. Echo them out before your database query to see what they contain. Quote Link to comment Share on other sites More sharing options...
Dharmender Posted January 10, 2013 Author Share Posted January 10, 2013 Since there are only three differences (the three variables) between your PHP code and the code you are typing directly into mysql, it stands to reason that there is something different in the three variables than what you are expecting. Echo them out before your database query to see what they contain. I have echoed the variable it returns the value of variables correctly Quote Link to comment Share on other sites More sharing options...
xenophobia Posted January 10, 2013 Share Posted January 10, 2013 try echo $sql then copy the sql string and make a query in your database. See if you get 5 result as expected. =) Quote Link to comment Share on other sites More sharing options...
Dharmender Posted January 10, 2013 Author Share Posted January 10, 2013 try echo $sql then copy the sql string and make a query in your database. See if you get 5 result as expected. =) Thanks for the help. I have resolved it by using the below query in PHP $sql="select a.date,(select itemname from items where itemid=a.itemid) as item,a.quantity,a.rate,a.fat,a.amount from purchasedetails a where a.supplierid=".$supname." and (str_to_date(a.date,'%d-%m-%Y') between str_to_date('".$fromdate."','%d-%m-%Y') and str_to_date('".$todate."','%d-%m-%Y'))"; Quote Link to comment 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.