/    Sign up×
Community /Pin to ProfileBookmark

[RESOLVED] Getting the sum of database rows

I have a function that needs to return the sum of selected rows of a database.

[code=php]
$query = mysql_query(‘SUM(image_size) FROM image WHERE user_id = “‘.mysql_real_escape_string($_SESSION[‘user_id’]).'”‘);
[/code]

I’m pretty sure that line of code works. I’m stumped as far as how to display the value. I tried return mysql_result($query); as well as a few other things, but nothing worked.

Thanks everyone!

to post a comment
PHP

1 Comments(s)

Copy linkTweet thisAlerts:
@KingCobra220authorJul 31.2007 — I figured it out.
[code=php]
$query='SELECT SUM(image_size) FROM image WHERE user_id = "'.mysql_real_escape_string($_SESSION['user_id']).'"';
$result = mysql_query($query) or die(mysql_error());
$row = mysql_fetch_row( $result );
return $row[0];
[/code]
×

Success!

Help @KingCobra220 spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.18,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...