how to display value of mysql sum query
I am trying to grab the total amount of sales made for a a single month
$aug11 = mysql_query("SELECT SUM(price) FROM table WHERE sales_date LIKE '08/%/2011' ");
开发者_JAVA技巧when I echo $aug11
I get a resource id# error. I also tried to do this
$test1 = mysql_fetch_array($aug11);
and when I echo $test1
it just says "Array".
Is it absolutely necessary to place a GROUP by sales_date in the original query and then have a while loop that grabs the array and lets me echo the values?
I don't really need any other value except the sum of 'price' for the month of August.
Can someone please explain to me how I can display the value I need without a while loop?
use this to get the sum value
$test1[0];
echo $test1[0]; // returns the sum
$aug11 is not a query string, it is the result of a query. When you fetch an array from the result, it is an array of values, not a scalar. I think you want to do this:
$query = "SELECT SUM(price) FROM table WHERE sales_date LIKE '08/%/2011'";
$aug11 = mysql_query($query);
$row = mysql_fetch_array($aug11);
$test1 = $aug11[0];
精彩评论