blueman378 Posted December 30, 2007 Share Posted December 30, 2007 hi guys , i have a table with a column called gPlays, i was wondering is it possible to add all the values together and echo it? btw they are all smallint Quote Link to comment https://forums.phpfreaks.com/topic/83703-solved-sum-mysql-data/ Share on other sites More sharing options...
tapos Posted December 30, 2007 Share Posted December 30, 2007 Use this: <?php // Make a MySQL Connection $query = "SELECT SUM('fieldname') FROM 'tablename'"; ?> Thanks -- Tapos Pal Quote Link to comment https://forums.phpfreaks.com/topic/83703-solved-sum-mysql-data/#findComment-425876 Share on other sites More sharing options...
blueman378 Posted December 30, 2007 Author Share Posted December 30, 2007 would this echo it? $q = "SELECT sum( gPlays ) FROM " . Games . " "; $result = $database->query($q) or die("Error: " . mysql_error()); database connect ect is done elsewhere Quote Link to comment https://forums.phpfreaks.com/topic/83703-solved-sum-mysql-data/#findComment-425878 Share on other sites More sharing options...
raku Posted December 30, 2007 Share Posted December 30, 2007 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. Quote Link to comment https://forums.phpfreaks.com/topic/83703-solved-sum-mysql-data/#findComment-425879 Share on other sites More sharing options...
blueman378 Posted December 30, 2007 Author Share Posted December 30, 2007 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? Quote Link to comment https://forums.phpfreaks.com/topic/83703-solved-sum-mysql-data/#findComment-425887 Share on other sites More sharing options...
blueman378 Posted December 30, 2007 Author Share Posted December 30, 2007 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? Quote Link to comment https://forums.phpfreaks.com/topic/83703-solved-sum-mysql-data/#findComment-425899 Share on other sites More sharing options...
blueman378 Posted December 30, 2007 Author Share Posted December 30, 2007 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; ?> Quote Link to comment https://forums.phpfreaks.com/topic/83703-solved-sum-mysql-data/#findComment-425908 Share on other sites More sharing options...
hitman6003 Posted December 30, 2007 Share Posted December 30, 2007 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'); } Quote Link to comment https://forums.phpfreaks.com/topic/83703-solved-sum-mysql-data/#findComment-426048 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.