noob question - integer output format, use of SUM in MySql

This is a discussion on noob question - integer output format, use of SUM in MySql within the MySQL Database forums, part of the Database Forums category; A bunch of small things are frustrating me in trying to do some simple stuff with MySQL and php. For ...


Go Back   Usenet Forums > Database Forums > MySQL Database

FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 04-25-2006
hph
 
Posts: n/a
Default noob question - integer output format, use of SUM in MySql

A bunch of small things are frustrating me in trying to do some simple
stuff with MySQL and php.

For example, I have am trying to sum the data in a particular field,
then output the result.

Here is the sequence I'm using in php to do so:

$dataquery="SELECT SUM(".$current_year.") FROM Donors WHERE Source LIKE
'%FF%'";
$result=mysql_query($dataquery);
$rowdata=mysql_fetch_row($result);
$datatotal=$rowdata[0];

Donors is of course the name of the table, $current_year is a variable
that is correctly 2006 (I've echoed it to make sure), and I'm looking
for the total of data in field 2006 where the associated field Source
contains the letters FF somewhere.

However, the result is displaying as 62186 when in fact it's 300.

Even if I use phpmyadmin and enter the query directly [SELECT SUM(2006)
FROM Donors WHERE Source LIKE '%FF%'], the result comes back
incorrectly, again 62186 instead of the expected 300.

So what am I doing wrong? The field 2006, which can hold values between
0 and 999,999, is defined as an INT 6. If I do the MySQL command
[SELECT FROM Donors WHERE Source LIKE '%FF%'] I do see just a small
number of rows, and the total in the fields 2006, computed in my head,
is 300.
Reply With Quote
  #2 (permalink)  
Old 04-25-2006
Gordon Burditt
 
Posts: n/a
Default Re: noob question - integer output format, use of SUM in MySql

>A bunch of small things are frustrating me in trying to do some simple
>stuff with MySQL and php.
>
>For example, I have am trying to sum the data in a particular field,
>then output the result.
>
>Here is the sequence I'm using in php to do so:
>
>$dataquery="SELECT SUM(".$current_year.") FROM Donors WHERE Source LIKE
>'%FF%'";
>$result=mysql_query($dataquery);
>$rowdata=mysql_fetch_row($result);
>$datatotal=$rowdata[0];
>
>Donors is of course the name of the table, $current_year is a variable
>that is correctly 2006 (I've echoed it to make sure), and I'm looking
>for the total of data in field 2006 where the associated field Source
>contains the letters FF somewhere.


Your query is:
SELECT SUM(2006) FROM Donors WHERE Source LIKE '%FF%';

In this context, 2006 is a *NUMBER*. `2006` is a field.
I advise not naming fields so they look like numbers.

>However, the result is displaying as 62186

This is 31 * 2006.
>when in fact it's 300.


>Even if I use phpmyadmin and enter the query directly [SELECT SUM(2006)
>FROM Donors WHERE Source LIKE '%FF%'], the result comes back
>incorrectly, again 62186 instead of the expected 300.


You should not be expecting 300 here.

>So what am I doing wrong? The field 2006, which can hold values between
>0 and 999,999, is defined as an INT 6. If I do the MySQL command
>[SELECT FROM Donors WHERE Source LIKE '%FF%'] I do see just a small
>number of rows, and the total in the fields 2006, computed in my head,
>is 300.


Gordon L. Burditt
Reply With Quote
  #3 (permalink)  
Old 04-26-2006
hph
 
Posts: n/a
Default Re: noob question - integer output format, use of SUM in MySql

In article <124rg075197ln6e@corp.supernews.com>,
gordonb.gylhm@burditt.org (Gordon Burditt) wrote:

[snip]
>
> Your query is:
> SELECT SUM(2006) FROM Donors WHERE Source LIKE '%FF%';
>
> In this context, 2006 is a *NUMBER*. `2006` is a field.
> I advise not naming fields so they look like numbers.


Yes, this fixed the problem. I renamed field 2006 to Yr2006 and
everything worked as I'd have expected. Thanks.
Reply With Quote
Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT +1. The time now is 09:56 AM.


Powered by vBulletin® Version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO 3.0.0