Wilian Brain Wilian Brain - 1 month ago 12
PHP Question

php count values column

i want to count the values column by users id

ex : i want to count values in mark column where id =1
means (30+70+30) how can do this after get the mark of users in sql code

database example

id mark user_id
----- ---- --------
1 30 1
2 60 3
3 70 1
4 10 2
5 30 1


My Code

<?php

$mark = $DB_con->prepare("SELECT id,mark,user_id FROM `tablename` WHER user_id=".$userRow['id']."");
$mark->execute();
foreach($mark->fetchAll() as $mar){
echo count($mar['mark']);
}

?>

Answer

Maybe something like this;

<?php 

$mark = $DB_con->prepare("SELECT id, mark, user_id FROM `tablename` WHERE user_id=$userRow['id']");
$mark->execute();
$total = 0;
foreach($mark->fetchAll() as $mar){
     $total += $mar['mark'];  
}
echo $total;

?> 
Comments