undefined is getting displayed through jquery

undefined is getting displayed through jquery

I have a PHP function which takes 3 parameters GroupId, Year and Month and checks in the database whether there are records less than the selected year and month for groupId and which returns database records. In my case only one record is returned. I am using json_encode(). Data is correctly displayed in json format but undefined is returned when I call this method using $.post() as described below: 

  1. public function actionGroupsavinginfo($id,$year,$month)
  2. {
  3. $group = Yii::$app->db->createCommand('SELECT count(*)
  4. FROM `groupsavingdetails`, groupdetails
  5. where groupdetails.GroupId=groupsavingdetails.GroupId
  6. and groupsavingdetails.Year<='.$year.'
  7. and groupsavingdetails.Month<='.$month.'
  8. and groupsavingdetails.GroupId='.$id
  9. )
  10. ->queryScalar();


  11. $groupsavingdetails = Yii::$app->db->createCommand('SELECT *
  12. FROM `groupsavingdetails`
  13. where
  14. groupsavingdetails.Year<='.$year.'
  15. and groupsavingdetails.Month<='.$month.'
  16. and groupsavingdetails.GroupId='.$id
  17. )
  18. ->queryAll();

  19. if ($group == 0) {

  20. echo $groupsavingdetails['OpeningBalance']=$groupsavingdetails['TotalValueofLoanGiven']=$groupsavingdetails['LoanRepaidUptilNow']=$groupsavingdetails['TotalValueOfLoanOutstanding']=0;
  21. }
  22. else {

  23. $data = array();

  24. foreach($groupsavingdetails as $groupsavingdetails1)
  25. {

  26. $data[] =$groupsavingdetails1;

  27. }

  28. return json_encode($data);

  29. }
  30. }


In the form I use jquery $.post() as follows

  1. $.post("index.php?r=groupsavingdetails/groupsavinginfo&id='.'"+$("select#groupsavingdetails-groupid").val()+"&year='.'"+$("input#groupsavingdetails-year").val()+"&month='.'"+$("select#groupsavingdetails-month").val(),function(data) {
  2. console.log(data);

  3.   
  4. $("input#groupsavingdetails-openingbalance").val(""+data[0].ClosingBalance);


  5. });

Here in console.log(data) data is displayed but data[0].ClosingBalance gives undefined.

What should I do?