-2

i want to get numbers from a database and use them in a sum. i don't really know how to start but i gave it go. i got an error when i echo just one number straight from the database which didn't help my self-a-steam. i don't know what i got wrong but i think it's a simple one. this is the code:

<?php

session_start();

$connection = mysql_connect("mysql15.000webhost.com", "a4987634_quiz", "**********")
or die (mysql_error());

mysql_select_db("a4987634_quiz", $connection)
or die (mysql_error());

$username = $_SESSION['username'];
$password = $_SESSION['password'];

$q1 = mysql_query ("SELECT 'q1' FROM users WHERE username='$username'");
$q2 = mysql_query ("SELECT 'q2' FROM users WHERE username='$username'");
$q3 = mysql_query ("SELECT 'q3' FROM users WHERE username='$username'");
$q4 = mysql_query ("SELECT 'q4' FROM users WHERE username='$username'");
$q5 = mysql_query ("SELECT 'q5' FROM users WHERE username='$username'");

$result = $q1 + $q2 + $q3 + $q4 + $q5 ;

echo $result ;

?>

thanks.

5
  • Also check what mysql_query actually returns. You can not use the + operator with it. The manual is your friend: php.net/mysql_query. It's not helpful if you ask a question without providing the exact error description btw.
    – hakre
    Oct 28, 2012 at 13:38
  • 4
    Please, don't use mysql_* functions in new code. They are no longer maintained and the deprecation process has begun on it. See the red box? Learn about prepared statements instead, and use PDO or MySQLi - this article will help you decide which. If you choose PDO, here is a good tutorial.
    – hakre
    Oct 28, 2012 at 13:39
  • While you're on the manual pages, to which hakre provided a link, notice that use of mysql_* extension is highly discouraged, switch to PDO or mysqli_* instead Oct 28, 2012 at 13:39
  • the error code is Resource id#9
    – Diggerb
    Oct 28, 2012 at 13:50
  • It's not an error, you just don't know what are you doing. Please learn some basics about how to fetch data from from mysql in php before asking questions Oct 28, 2012 at 14:03

3 Answers 3

3

Assuming there is only one row in users for each username then:

Select q1 + q2 + q3 + q4 + q5 as total from users where username = '$username'

If there are more than one then you maybe want

Select Sum(q1 + q2 + q3 + q4 + q5) as total from users where username = '$username'
4
  • +1 + to add columns, sum to add rows
    – Andomar
    Oct 28, 2012 at 13:41
  • i get an error. Resource id#9
    – Diggerb
    Oct 28, 2012 at 13:51
  • I don't know php, but php.net/manual/en/function.mysql-query.php makes it pretty obvious how to use the result of mysql_query, as well as telling you not to use mysql_ functions for new code...
    – Laurence
    Oct 28, 2012 at 14:14
  • No need to use curly brackets.
    – Kermit
    Oct 29, 2012 at 17:43
1

I think you can use simple operators here -

SELECT (q1+q2+q3+q4+q5) AS total FROM users WHERE username='$username'
5
  • It seems OP's current issue is summing of resultsets but not actural row field values. Oct 28, 2012 at 13:40
  • 1
    -1 But sum takes only one parameter. This gives a syntax error.
    – Andomar
    Oct 28, 2012 at 13:40
  • i got an error when i tried this
    – Diggerb
    Oct 28, 2012 at 13:53
  • It would help if we knew what the error is....
    – Lix
    Oct 28, 2012 at 13:54
  • You simply haven't fetched the results from the query. You'll have to execute a mysql_fetch_assoc() on the object returned from the mysql_query() function.
    – Lix
    Oct 28, 2012 at 14:04
0

You can't add up resultsets directly but the specific field values read from each of them.
Add the field values read from each of the result set to sum up and then echo the result.

Example:

$result = 0;
$resultSetsArray = array( $q1, $q2, $q3, $q4, $q5 );  

foreach( $resultSetsArray as $rs ) {  
  while( $row = mysql_fetch_array( $rs ) ) {  
    $result = $result + $row[ 1 ];  
    break; // while  
  } // while rs  
} // for each  

echo "result : " . $result;

You have better ways of summing of column values of a database table column.
It seems you need more learning and practice both in PHP and MySQL.

Not the answer you're looking for? Browse other questions tagged or ask your own question.