title: PHP encode a partial set of MySQL data into JSON
body: <p>I know how to encode all the data from an SQL query into JSON using PHP, but don't know How to encode a partial set.</p>
  
  <p>In Android (the client), I send an HTTP request which goes something like this:</p>
  
  <pre><code>public interface DataAPI {
  
         @GET("/top500")
          public void getResult(@Query("start") int start, @Query("count") int count,
                                Callback&lt;Top500&gt; response);
      }
  </code></pre>
  
  <p>As you can see, I am using start and count in Android code.
  I have around 500 records in my table and I have successfully encoded all the data into JSON, using below PHP Script:</p>
  
  <pre><code>&lt;?php
  
  $con = mysqli_connect(HOST,USER,PASS,DB);
  
  $sql = "select * from Persons";
  
  $res = mysqli_query($con,$sql);
  
  $result = array();
  
  while($row = mysqli_fetch_array($res)){
  array_push($result,
  array(
  'id'=&gt;$row[0],
  'name'=&gt;$row[1]    
  ));
  }
  
  echo json_encode(array("result"=&gt;$result));
  
  mysqli_close($con);
  
  ?&gt;
  </code></pre>
  
  <p>JSON Result:</p>
  
  <pre><code>{"result": [{ ... }]}
  </code></pre>
  
  <p>But I need to encode mysql data 20 by 20 like this:</p>
  
  <pre><code>{"count": 20, "start": 0, "total": 500, "result": [{ ...}]}
  </code></pre>
  
  <p>What's missing is how to use the <em>start</em> and <em>count</em> parameters to get a partial set in order to get slices of the data.</p>