kevincro Posted November 17, 2007 Share Posted November 17, 2007 I'm trying to return the sum of a field in a mysql database using PHP. For some reason the function doesn't want to return a value. It gives me a result resource message. The following is the code that I'm using. $query5 = "SELECT SUM(num) FROM bappurchased WHERE bapnumber='$data_key'"; $result5=mysql_query($query5); $data_key is a variable that is carried over from a previous page and it holds a numerical value that corresponds to the rows that contain the field that I'm trying to sum. I'm trying to return the sum of these fields and use the value for further calculations on the same page. Quote Link to comment Share on other sites More sharing options...
Orio Posted November 17, 2007 Share Posted November 17, 2007 <?php $query5 = "SELECT SUM(num) FROM bappurchased WHERE bapnumber='$data_key'"; $result5=mysql_query($query5); $res5 = mysql_fetch_array($result5); echo $res5['SUM(num)']; ?> Orio. Quote Link to comment Share on other sites More sharing options...
kevincro Posted November 17, 2007 Author Share Posted November 17, 2007 Thank you so much. That worked perfectly. I looked up the SUM function on both the PHP and MySQL websites first. I don't like to post here unless I've made an effort to find out on my own, but nothing gave me any help. I really appreciate it. KC Quote Link to comment Share on other sites More sharing options...
Orio Posted November 17, 2007 Share Posted November 17, 2007 No problem I personally also always look in the manuals and over the web before asking for help- and that's how all people should do... Although sometimes we don't search in the correct places, so it's good to ask Orio. Quote Link to comment 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.