Jump to content

[SOLVED] Aggregate Functions


mwl707

Recommended Posts

Hi Can anyone help please .

 

I am trying to write a function. The code is passed a variable which is used in a mysql query. (Code below) In the second to last line I always get 0 as a result when I know the result should be 3. If i use the following code I get the correct result

 

$screen_size = "i100s" ;

$result = mysql_query("SELECT SUM($screen_size) FROM event WHERE jobstatus = 'confirmed' ")  ;

 

while($row = mysql_fetch_array($result)){

$res = $row['SUM($screen_size)'] ;

 

echo "Total ". $row['jobstatus']. " = ". $res ;

echo "<br />";

 

In the second to last line I always get 0 as a result when I know the result should be 3. If i use the following code I get the correct result

 

$screen_size = "i100s" ;

$result = mysql_query("SELECT SUM($screen_size) FROM event WHERE jobstatus = 'confirmed' ")  ;

 

while($row = mysql_fetch_array($result)){

 

$res = $row['SUM(i100s)'] ;

 

echo "Total ". $row['jobstatus']. " = ". $res ;

echo "<br />";

 

Can anyone offer some advice ?

 

Thanks Mick

 

Link to comment
Share on other sites

Doesn't matter.  Probably is.  The problem is that you're using single quotes (' ') around your array key ($row['SUM($screen_size)']) so variables don't interpolate.  Use double quotes.

That's not it. You can't run SUM outside of a query. o.O Well, unless it's a function.

Link to comment
Share on other sites

If it's in quotation marks, it wouldn't matter.

 

<?php
echo "substr('test', 0, 10);";

 

He could (and probably should) be using an alias in the query, but in reality, it doesn't matter.  Just more typing if he doesn't. xD

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.