db-laravel-query-builder.php 987 B

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