-3

I have this field on my database

email, addition so what i want is that WHERE email='$email' it will look that the data user had for addition. now for addition field we have 100,100,1000

how can i compute this with the echo 1200

my code is NOTE this is just a sample scriptcode i can't copy and paste my code here because it was long. so if you find typo error ignore it.

$sql = mysql_query( "SELECT * FROM user WHERE email = '$email' ") or die(mysql_error());
while ( $row = mysql_fetch_array($sql) ){
   echo $row[addition] ++;
}

yes i know its not correct format. what should i change? do i need to use for or foreach? in order to add those computation?

tghanks

4
  • 3
    You should normalize your database first. Look it up on google. Commented Jul 19, 2012 at 10:15
  • Without code no one helps you becuase what you are done it is not clear to them wants help you Commented Jul 19, 2012 at 10:17
  • Alright thank you so much, next time will complete the code Commented Jul 19, 2012 at 10:21
  • some people had a good understanding while some are so slow. thanks Commented Jul 19, 2012 at 10:27

2 Answers 2

2

with correct (normalized) data structure you can do:

SELECT SUM(addition) AS total FROM users WHERE email = '$email'

if you store it as csv in a column as Ignas says in his commentes, you can either fix your database structure or use php:

$sum = array_sum(explode(',', $row['addition']));
3
  • Thanks that was im looking for very simple answer with so much knowledge NOT a waste of TIME... thank you sir. Commented Jul 19, 2012 at 10:26
  • array_sum() ... nice, completely overlooked that function :) Commented Jul 19, 2012 at 10:33
  • @CD001 One of the best thing with answering questions on SO is that you can see how other people solves the same problem, and learn new ways to do the stuff you alredy know Commented Jul 19, 2012 at 10:39
0

Somthing like?

$sql = mysql_query( "SELECT * FROM user WHERE email = '$email' ") or die(mysql_error());

while ( $row = mysql_fetch_array($sql) ) {
  $iAdditionSum = 0;
  $aAddition = explode(',', $row['addition']);

  foreach($aAddition as (int) $iAddition) {
    $iAdditionSum += $iAddition;
  }

  echo $iAdditionSum;
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.