Jump to content

Mysql query in PHP


Dharmender

Recommended Posts

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.

Link to comment
https://forums.phpfreaks.com/topic/272924-mysql-query-in-php/
Share on other sites

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.

Link to comment
https://forums.phpfreaks.com/topic/272924-mysql-query-in-php/#findComment-1404567
Share on other sites

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

Link to comment
https://forums.phpfreaks.com/topic/272924-mysql-query-in-php/#findComment-1404570
Share on other sites

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'))";

Link to comment
https://forums.phpfreaks.com/topic/272924-mysql-query-in-php/#findComment-1404575
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.