i have this code which i use in order to extract first all users that exist in my database,
then, i query each of them, to see how many points they have calculated.
My issue is, the users that have points allocated the query extracts correct information, but if a user has not any points, then, all those users won’t appear 0 points, but another user’s points are given to them..
What is wrong with my code?
//here i get all user id's
$query = 'SELECT * FROM users';
$result = mysql_query($query) or die(mysql_error());
$id_user = array();
$phone = array();
$name = array();
while($row = mysql_fetch_array($result)){
$id_user[]=$row['user_id'];
$phone[]=$row['phone'];
$name[]=$row['first_name'];
}
//here i get all points collected for each user
foreach ($id_user as $user_id) {
$queryuser ='SELECT SUM(basket_value) as total_sum FROM retailer WHERE user_id="'.$user_id.'"';
$resultuser = mysql_query($queryuser) or die(mysql_error());
echo "<table>";
while($row = mysql_fetch_array($resultuser)){
$total += $row['total_sum'];
echo "<tr>";
echo "<td>";
echo $total;
echo "</td>";
echo "<td>";
echo $user_id;
echo "</td>";
echo "</tr>";
}
}
echo "<table>";
Your issue:
all those users won't appear 0 points, but another user's points are given to themBecause you need to initialize
$total=0;beforewhile($row = mysql_fetch_array($resultuser)){