0

this is my php code,

    $count=mysql_num_rows($query);

if ($count > 0) {
  // output data of each row
  $foodList[] = array();
  while($row =mysqli_fetch_assoc($result))
  {
      $foodList[] = $row;
  }
} else {}

echo json_encode($foodList);

this is my js code:

var $promise = $http.post('foodList.php');

$promise.then(function(msg){
  var foodList = msg.data;

  if (foodList) 
    {
      //$scope.foodList = foodList;
      alert(foodList);
    }
  else
    {
      //$scope.msg = "Error user name or password";
    }

this is output:

$promise.then(function(msg){*msg = Object {data: Array[1], status: 200, config: Object, statusText: "OK"}* var foodList = msg.data;*foodList = [Array[0]]*

So: actually 3 data in my data base, but in output just only Array[1]? How to fix it ? THX

5
  • change $foodList[] = array(); to $foodList = array(); Commented Oct 6, 2015 at 5:47
  • the output is become to array[0] Commented Oct 6, 2015 at 5:53
  • var_dump($foodList) in your php file and make sure your php file show 3 data Commented Oct 6, 2015 at 5:55
  • if i set php echo the $count, i will get 3 for return, y this happened? Commented Oct 6, 2015 at 6:11
  • you count the $query but in while loop use $result why? Commented Oct 6, 2015 at 6:18

2 Answers 2

0

Your count the $query variable so in your while loop use $query variable

<?php
$count=mysql_num_rows($query);

if ($count > 0) {
  // output data of each row
  $foodList = array();
  while($row =mysqli_fetch_assoc($query))
  {
      $foodList[] = $row;
  }
}

echo json_encode($foodList);

?>

0
0

Your php syntax and the usage of commands are wrong in some places. Here is the corrected code. Please compare and see the difference.

$count=mysql_num_rows($result);

if ($count > 0) {

  // output data of each row
  $foodList = array();

  while($row =mysqli_fetch_assoc($result))
  {        
      array_push($foodList, $row);
  }
}

echo json_encode($foodList);

This should work if you are selecting the rows correctly.

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.