0

I create a web service (API) using PHP I am trying to display all records of my database table using array via JSON and xml but it is only display one record or it is getting respond for only one record. Any idea would be useful.

Thanks

   $query = "SELECT * FROM students";


  $result = mysqli_query($con, $query);

  while($rows = mysqli_fetch_array($result))
    {
      $fullrecord =  $rows['ID'] . " " . $rows['StudentName'] . " " .  $rows['Age'];

      $response['data'] =  $fullrecord;  
    }


   //Return Response to browser

  deliver_response($_GET['format'], $response);
6
  • 2
    To convert an array to JSON just use the following: $json = json_encode($your_array); Commented Sep 19, 2014 at 14:47
  • @burmat then you can use print_r to see it if you want to debug. Commented Sep 19, 2014 at 14:48
  • I think you are screwing up in your while loop. You are creating an array each time you loop and then re-setting it to something different. I can't help with the XML right now, but can help with the JSON if you want Commented Sep 19, 2014 at 14:51
  • It is displaying the result only for one record .How I can display it for all records? Commented Sep 19, 2014 at 14:59
  • @ZinaDweeikat See my answer below. You are overwriting your value each time you loop in your while. That is why you are only returning the last row. Commented Sep 19, 2014 at 15:09

2 Answers 2

0

I think you are having issues not with encoding/decoding your array, but how you are actually creating your array. In your while loop, you are creating $response and setting it to the current row. Ultimately, this will only provide you with the last row that your query processes (which is fine if you know that there will be only one row returned). To avoid this, declare an empty array: $response = array();. Make sure that $response['data'] is not overwritten multiple times by properly making it multidemensional: $response['data'][]. This ensures each row is appended to the array and does not overwrite a value already stored. Try the following out for your JSON functionality:

<?php

    $query = "SELECT * FROM students";
    $result = mysqli_query($con, $query);
    $response = array();
    while($rows = mysqli_fetch_array($result)) {
        $fullrecord =  $rows['ID'] . " " . $rows['StudentName'] . " " .  $rows['Age'];
        $response['data'][] =  $fullrecord;
    }

    $response = json_encode($response);

    deliver_response($_GET['format'], $response);

?>

UPDATE PER YOUR COMMENT:

The following code will simulate your query. It will use $table which is just a simple array. Each item in it will simulate your rows in your db table. It will loop through each item in the array and push it into a new associative array called $response. This is directly related to your problem so pay attention here. Once the loop is done (all rows are pushed into $response), we encode it and return it:

<?php

    // this simulates your db table
    $table = array('row 1', 'row 2', 'row 3', 'row 4');

    $response = array();

    // this simulates your `while` loop
    foreach ($table as $row) {
        // push your value into the associative array
        $response['data'][] = $row;
    }

    // print_r will yield the following:
    // Array ( [data] => Array ( [0] => row 1 [1] => row 2 
            // [2] => row 3 [3] => row 4 ) )

    $json_response =  json_encode($response);
    // another print_r will yield the following:
    // {"data":["row 1","row 2","row 3","row 4"]}

    return $json_response;
?>

Now that we have the JSON data, we can read it on the other side with using something similar to what follows. First we will decode it, grab the array [data], and we will loop through it to print the results on the screen:

<?php
    $response = json_decode($json_response);
    // pull the data:
    $data = $response->{'data'};
    foreach ($data as $d) {
        echo $d . '<br />';
    }
?>
Sign up to request clarification or add additional context in comments.

1 Comment

@ZinaDweeikat i just updated my answer with code simulating your table. I did it this way because it works - I tested it. Just replace the for loop in my code with your while loop and make sure your query isn't failing, because this code won't
0

For XML see thethis question.
And for JSON use associative array:

$json = json_encode($assoc_array);

2 Comments

But its only displaying the result for one record ? I would like to display it for all records? any idea ?
Make an array of all records

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.