0

I'm using this code to convert database data to json but I am getting undefined index error in this line $total[$name] += $score. Thank you in advance.

 <?php
 require_once('conn.php');

 $sql = "select name,score from highscores ";

 $res = mysqli_query($con,$sql); 


if (mysqli_num_rows($res) > 0) {
$result = array();
while($item = mysqli_fetch_assoc($res)){

    $result[]=$item;
}

$total=array();
foreach ($result as $row) {
  $name = $row['name'];
  $score = $row['score'];
  $total[$name] += $score;


$newArray = array();
foreach ($total as $name => $score) {

  array_push($newArray,array('name' => $name, 'score' =>$score));
} 
}
echo json_encode(array("result"=>$newArray));
}
  • $total[$name] += $score; it is actually $total[$name] = $total[$name] + $score; but first time $total[$name] is not defined. thats why you are getting error. – Bhaskar Jain May 01 '17 at 11:21
  • I solved my problem.Thank you very much. –  May 01 '17 at 11:25

0 Answers0