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

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • hph

    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="SEL ECT SUM(".$current_ year.") FROM Donors WHERE Source LIKE
    '%FF%'";
    $result=mysql_q uery($dataquery );
    $rowdata=mysql_ fetch_row($resu lt);
    $datatotal=$row data[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.
  • Gordon Burditt

    #2
    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[color=blue]
    >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="SE LECT SUM(".$current_ year.") FROM Donors WHERE Source LIKE
    >'%FF%'";
    >$result=mysql_ query($dataquer y);
    >$rowdata=mysql _fetch_row($res ult);
    >$datatotal=$ro wdata[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.[/color]

    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.
    [color=blue]
    >However, the result is displaying as 62186[/color]
    This is 31 * 2006.[color=blue]
    >when in fact it's 300.[/color]
    [color=blue]
    >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.[/color]

    You should not be expecting 300 here.
    [color=blue]
    >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.[/color]

    Gordon L. Burditt

    Comment

    • hph

      #3
      Re: noob question - integer output format, use of SUM in MySql

      In article <124rg075197ln6 e@corp.supernew s.com>,
      gordonb.gylhm@b urditt.org (Gordon Burditt) wrote:

      [snip][color=blue]
      >
      > 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.[/color]

      Yes, this fixed the problem. I renamed field 2006 to Yr2006 and
      everything worked as I'd have expected. Thanks.

      Comment

      Working...