0

I have a table with two fields, named credithour and gradepoint.

I want to add the data from each row of credithour with gradepoint.

My code :

$totalcredithour = 0;                           
$GPA = "SELECT * FROM $dept_stu_id WHERE sessionyear = '$sessionyear'" or die (mysql_error());
$resultGPA = mysql_query($GPA);
while($data = mysql_fetch_array($resultGPA))
{ 
    $credithour = "$data[credithour]";
    $gradepoint = "$data[gradepoint]";
    echo $totalcredithour += $credithour;   
}

What am I doing wrong?

It added data, but showing the first credit hour also besides result. Suppose, here i entered two data 3 and 5. When I run this code it echo 38. Which means "3+5=8" and 1st credithour = "3".

2
  • I tried to edit your question, but it wasn't very clear to begin with. Let me know if I missed anything. Commented Nov 15, 2011 at 21:20
  • i want to retrieve the data from two column, and add each row of data from those column separately. That is i try to doing. Commented Nov 15, 2011 at 21:31

3 Answers 3

1

Actually other answer are wrong, you're doing or die(mysql_error()); next to a string, that's not right.

Your code should be like this..

$totalcredithour = 0;                           
$GPA = "SELECT * FROM `".$dept_stu_id."` WHERE sessionyear = `".$sessionyear."` ";
$resultGPA = mysql_query($GPA) or die(mysql_error());
while($data = mysql_fetch_array($resultGPA, MYSQL_ASSOC))
{ 
    $credithour = $data['credithour'];
    $gradepoint = $data['gradepoint'];

    echo $totalcredithour += $credithour; //Make sure `$totalcredithot` isn't null/doesn't exist.
}
Sign up to request clarification or add additional context in comments.

4 Comments

Still it doesn't working. Yes, "$totalcredithour" is exist and null.
Weird, make sure those columns exists. Also, change echo $totalcredithour += $credithour; to echo $credithour;. So we can check if it's returning null.
yeah, I have already change that and returning same result. I agree, its weird. I have just edited my post, see that please. Thnx
I got it. Thank you for replying.
0

try something like this in your while loop:

$credithour = $data['credithour'];
$gradepoint = $data['gradepoint'];

2 Comments

is your table called $dept_stu_id? it might just be dept_stu_id - the $ designates a php variable.
Nope, "$dept_stu_id" is a posted variable, which i used as a table name. It isn't a related about this problem dear.
0

Try it like this

I have edited the answer and added the floatval function

$totalcredithour = 0;                           
  $GPA = "SELECT * FROM `".$dept_stu_id."` WHERE sessionyear = '".$sessionyear."'" or die (mysql_error());
 $resultGPA = mysql_query($GPA);
while($data = mysql_fetch_array($resultGPA))
{ 
$credithour = $data['credithour'];
$gradepoint = $data['gradepoint'];
echo $totalcredithour += floatval($credithour);   
 }

1 Comment

The related data are retrieve successfully, but problem is they don't add. :(

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.