开发者

calculation using date range is not giving output in php

My php script is taking two dates from a form and trying to get the profit calculation between these two dates.

My profit table in mysql are :id(int),order_profit(float) and order_date(date)

the form date field names are date_from and date_to

The code for the php script below:

<?php
include("dbconnect.php");
$from_date=$_POST['date_from'];
$to_date=$_POST['date_to'];
echo "Small date : ".$from_date."<br>";
echo "Large date : ".$to_date;

    $count=1;


 $queryA=mysql_query("SELECT SUM(order_profit) FROM profit  WHER开发者_如何学CE profit_date BETWEEN STR_TO_DATE($from_date, '%Y/%m/%d') AND STR_TO_DATE($to_date, '%Y/%m/%d') ");

while($row = mysql_fetch_array($queryA)){
        $count++;

         echo "Total Profit"." =". $row['SUM(order_profit)'];
      }
    echo $count;    
           echo "<br />";


?>

when the outcome shows it displays two dates and count value 2. That means the while loop body is executed once but no profit value is shown.But no error is shown.

Please help me in this regard


Check that you're using the correct column name:

var_dump($row);

Or name it explicitly:

SELECT SUM(order_profit) as sum


You can also echo your SQL query and run it in your SQL editor to see what it's actually returning. I've had a number of untraceable php issues that were resolved by checking if my query was valid.


Check this out. IMHO dates must be be enquoted. Check also the mysql errors in case there are some: mysql_error() is your friend.

<?php
include("dbconnect.php");
$query = "SELECT SUM(order_profit) as profitsum FROM profit ";
$query .= "WHERE profit_date BETWEEN STR_TO_DATE('".$_POST['date_from']."', '%Y/%m/%d') ";
$query .= "AND STR_TO_DATE('".$_POST['date_to']."', '%Y/%m/%d')";

// to check the query
echo $query ."<br/>";

$result = mysql_query($query) or die(mysql_error());

$row = mysql_fetch_array($result);
echo "Total Profit: ".$row['profitsum']."<br/>;

?>
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜