dbraw.php 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. <?php
  2. //
  3. // Database Test
  4. //
  5. function dbraw() {
  6. // Database connection (TODO: When it works, use PDO instead)
  7. $link = mysql_connect('TFB-database', 'benchmarkdbuser', 'benchmarkdbpass');
  8. mysql_select_db('hello_world', $link);
  9. // Read number of queries to run from URL parameter
  10. $query_count = 1;
  11. $is_multi = isset($_GET['queries']);
  12. if ($is_multi) {
  13. $query_count = (int)$_GET['queries'];
  14. if ($query_count > 500) {
  15. $query_count = 500;
  16. } else if ($query_count < 1) {
  17. $query_count = 1;
  18. }
  19. }
  20. // Create an array with the response string.
  21. $arr = array();
  22. $id = mt_rand(1, 10000);
  23. // For each query, store the result set values in the response array
  24. while (0 < $query_count--) {
  25. $result = mysql_query("SELECT randomNumber FROM World WHERE id = $id", $link);
  26. // Store result in array.
  27. $arr[] = array('id' => $id, 'randomNumber' => mysql_result($result, 0));
  28. $id = mt_rand(1, 10000);
  29. }
  30. mysql_close($link);
  31. // Set content type
  32. header("Content-type: application/json");
  33. // Use the PHP standard JSON encoder.
  34. // http://www.php.net/manual/en/function.json-encode.php
  35. if ($is_multi) {
  36. echo json_encode($arr);
  37. } else {
  38. echo json_encode($arr[0]);
  39. }
  40. }
  41. dbraw();
  42. ?>