By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,784 Members | 3,274 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,784 IT Pros & Developers. It's quick & easy.

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

P: n/a
hph
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.
Apr 25 '06 #1
Share this Question
Share on Google+
2 Replies


P: n/a
>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
Apr 25 '06 #2

P: n/a
hph
In article <12*************@corp.supernews.com>,
go***********@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.
Apr 26 '06 #3

This discussion thread is closed

Replies have been disabled for this discussion.