Repeating table sum in PHP

I have 2 columns in a table called Points. 2 columns are UserPoints and UserID.

I want to be able to repeat the total number of points that the user has.

I have something like this, but I don’t think it is right.

$getTotalPoints = mysql_query("SELECT SUM(UserPoints) FROM `Points` WHERE `UserID` = '1'") or die(mysql_error()); 
$totalPoints = mysql_fetch_array($getTotalPoints);

When I repeat the previous statement, repeating "$ totalPoints", I get an "Array".

Does anyone know the correct query for this?

+5
source share
4 answers

Array, , $totalPoints. , , mysql_fetch_array(), , . var_dump() $totalPoints, :

Array
(
    [0] => 12345
    [SUM(UserPoints)] => 12345
)

, , 0 , SUM(UserPoints), echo $totalPoints[0] echo $totalPoints['SUM(UserPoints)'].

mysql_result(). , . . , mysql_fetch_array() :

$totalPoints = mysql_result($result, 0);

mysql_result() PHP .

, mysql_ *, . , PDO , , mysqli, . , ... , . , , . , .

, ... !

+4

mysql_fetch_array , , . . , , echo $totalPoints[0];

,

$getTotalPoints = mysql_query("SELECT SUM(UserPoints) total FROM `Points` 
        WHERE `UserID` = '1'") or die(mysql_error()); 
$totalPoints = mysql_fetch_array($getTotalPoints);
echo $totalPoints['total'];
+1

mysql_fetch_array . $totalpoints .

:

echo $totalPoints[0];

mysql, php.

mysql_fetch_array

+1

The result line is an array with as many elements as you got in SELECT. In your case, you have only 1 element (amount). Therefore, you should: echo $totalPoints[0]; If you need to debug such problems, I recommend that you read about the print_r function.

+1
source

All Articles