app.php 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. <?php
  2. function db()
  3. {
  4. global $pdo;
  5. $statement = $pdo->prepare("SELECT id,randomNumber FROM World WHERE id=?");
  6. $statement->execute([mt_rand(1, 10000)]);
  7. echo json_encode($statement->fetch(PDO::FETCH_ASSOC), JSON_NUMERIC_CHECK);
  8. }
  9. function query()
  10. {
  11. global $pdo;
  12. $statement = $pdo->prepare("SELECT id,randomNumber FROM World WHERE id=?");
  13. $query_count = 1;
  14. if ($_GET["queries"] > 1) {
  15. $query_count = min($_GET["queries"], 500);
  16. }
  17. while ($query_count--) {
  18. $statement->execute([mt_rand(1, 10000)]);
  19. $arr[] = $statement->fetch(PDO::FETCH_ASSOC);
  20. }
  21. echo json_encode($arr, JSON_NUMERIC_CHECK);
  22. }
  23. function update()
  24. {
  25. global $pdo;
  26. $query_count = 1;
  27. if ($_GET["queries"] > 1) {
  28. $query_count = min($_GET["queries"], 500);
  29. }
  30. $statement = $pdo->prepare("SELECT randomNumber FROM World WHERE id=?");
  31. $updateStatement = $pdo->prepare("UPDATE World SET randomNumber=? WHERE id=?");
  32. while ($query_count--) {
  33. $id = mt_rand(1, 10000);
  34. $statement->execute([$id]);
  35. $world = ["id" => $id, "randomNumber" => $statement->fetchColumn()];
  36. $updateStatement->execute(
  37. [$world["randomNumber"] = mt_rand(1, 10000), $id]
  38. );
  39. $arr[] = $world;
  40. }
  41. echo json_encode($arr, JSON_NUMERIC_CHECK);
  42. }
  43. function fortune()
  44. {
  45. global $pdo;
  46. $fortune = $pdo->prepare("SELECT id,message FROM Fortune");
  47. $fortune->execute();
  48. $arr = $fortune->fetchAll(PDO::FETCH_KEY_PAIR);
  49. $arr[0] = "Additional fortune added at request time.";
  50. asort($arr);
  51. $html = "";
  52. foreach ($arr as $id => $message) {
  53. $message = htmlspecialchars($message, ENT_QUOTES, "UTF-8");
  54. $html .= "<tr><td>{$id}</td><td>{$message}</td></tr>";
  55. }
  56. echo "<!DOCTYPE html><html><head><title>Fortunes</title></head><body><table><tr><th>id</th><th>message</th></tr>$html</table></body></html>";
  57. }