db-eloquent.php 906 B

12345678910111213141516171819202122232425262728293031323334353637
  1. <?php
  2. require_once __DIR__ . '/boot-eloquent.php';
  3. // Read number of queries to run from URL parameter
  4. $query_count = 1;
  5. $query_param = isset($_GET['queries']);
  6. if ($query_param && $_GET['queries'] > 0) {
  7. $query_count = $_GET['queries'] > 500 ? 500 : $_GET['queries'];
  8. }
  9. // Create an array with the response string.
  10. $arr = array();
  11. // For each query, store the result set values in the response array
  12. $query_counter = $query_count;
  13. while (0 < $query_counter--) {
  14. // Choose a random row
  15. // http://www.php.net/mt_rand
  16. $id = mt_rand(1, 10000);
  17. $world = World::find($id);
  18. // Store result in array.
  19. $arr[] = $world->toArray();
  20. }
  21. if ($query_count === 1 && !$query_param) {
  22. $arr = $arr[0];
  23. }
  24. // Set content type
  25. header("Content-type: application/json");
  26. // Use the PHP standard JSON encoder.
  27. // http://www.php.net/manual/en/function.json-encode.php
  28. echo json_encode($arr);