Jump to content

[SOLVED] sum mysql data


blueman378

Recommended Posts

From the code you entered, it looks like you're using a framework. I'm not sure which you're using, but this is how you could do it:

 

$result = mysql_query($query);
if($row = mysql_fetch_array($result))
{
// access $row array to get info from database, set it to a variable (i.e. $var)
}
echo $var;

 

Hope this helps, it would probably be better to find out how your specific framework does it though, since it may automatically sanitize your queries and what not. Not really necessary for this query, but may be for others you're doing.

 

Link to comment
Share on other sites

well for example heres a working query:

<?php
$game = $_GET['game'];
    global $database;
    $q = "SELECT * FROM " . Games . " //Games is defined in a constants page
          WHERE gName = '$game'
          ORDER BY `gName` ASC
         ";   
    $result = $database->query($q) or die("Error: " . mysql_error());
    /* Error occurred, return given name by default */
    $num_rows = mysql_numrows($result);
    if( $num_rows == 0 ){
      return 'Game Not Found!';
    }
    while( $row = mysql_fetch_assoc($result) ) {
    echo $row[gName];
    }

?>

 

basically this follows this

 

does that give you enough info?

Link to comment
Share on other sites

ok so this works to retrieve the values:

$q = "SELECT * FROM " . Games . " ";   
    $result = $database->query($q) or die("Error: " . mysql_error());
    /* Error occurred, return given name by default */
    $num_rows = mysql_numrows($result);
    if( $num_rows == 0 ){
      return 'Game Not Found!';
    }

    while( $row = mysql_fetch_assoc($result) ) {
     echo $row[gplays];
    }

but the moment i try

$q = "SELECT SUM('gplays') FROM " . Games . " ";   
    $result = $database->query($q) or die("Error: " . mysql_error());
    /* Error occurred, return given name by default */
    $num_rows = mysql_numrows($result);
    if( $num_rows == 0 ){
      return 'Game Not Found!';
    }

    while( $row = mysql_fetch_assoc($result) ) {
     echo $row[gplays];
    }

i get a blank,

any ideas?

Link to comment
Share on other sites

well i gave up on that and just put this together

 

<?php
    global $database;
    $q = "SELECT * FROM " . Games . " ";   
    $result = $database->query($q) or die("Error: " . mysql_error());
    /* Error occurred, return given name by default */
    $num_rows = mysql_numrows($result);
    if( $num_rows == 0 ){
      return 'Game Not Found!';
    }
    $rate = 0;
    while( $row = mysql_fetch_assoc($result) ) {
     $rate = $rate + $row[gplays];
    }
echo $rate;
?>

Link to comment
Share on other sites

MySQL will return the field name as "SUM(gplays)" when the sum operation is used, rather than just "gplays".  Use the "AS" SQL command to give it a name:

 

    $q = "SELECT SUM('gplays') AS total_plays FROM " . Games . " ";   
    $result = $database->query($q);  // no need for the "die" here, there is error checking below
    
    /* check for errors */
    if ( mysql_num_rows($result) != 1 ) {
        return 'Unable to get total plays';
    } else {
        return mysql_result($result, 0, 'total_plays');
    }

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.