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
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
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
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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.