1

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

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?

4
  • 1
    you are fetching array, thus test1 is array.. consider var_dump($test1); or $test1[0] to access the value you are looking for.. Commented Sep 7, 2011 at 19:48
  • 1
    RTFM: php.net/mysql_fetch_array when a function is called 'fetch_array', perhaps you should assume that just maybe it actually returns (surprise!) an array. Commented Sep 7, 2011 at 19:50
  • 1. Salesdate should be a date, not a varchar 2. don't use like, use where sales_date BETWEEN '2011-08-01' AND '2011-08-31' 3. put an index on sales_date if you want fast queries. Commented Sep 7, 2011 at 20:50
  • Thanks for all your comments! We could've done without the snide condescending tone of some of them (surprised Marc B?), but the help is still very much appreciated. have a great weekend guys Commented Sep 9, 2011 at 14:46

3 Answers 3

4

use this to get the sum value

$test1[0];

Sign up to request clarification or add additional context in comments.

Comments

0
echo $test1[0]; // returns the sum

Comments

0

$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];

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.