updateraw.php 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. <?php
  2. header('Content-type: application/json');
  3. // Database connection
  4. // http://www.php.net/manual/en/ref.pdo-mysql.php
  5. $pdo = new PDO('mysql:host=tfb-database;dbname=hello_world', 'benchmarkdbuser', 'benchmarkdbpass', [
  6. PDO::ATTR_PERSISTENT => true,
  7. PDO::ATTR_EMULATE_PREPARES => false
  8. ]);
  9. // Read number of queries to run from URL parameter
  10. $query_count = 1;
  11. if ($_GET['queries'] > 1) {
  12. $query_count = $_GET['queries'] > 500 ? 500 : $_GET['queries'];
  13. }
  14. // Create an array with the response string.
  15. $arr = [];
  16. // Define query
  17. $statement = $pdo->prepare('SELECT randomNumber FROM World WHERE id = ?');
  18. $updateStatement = $pdo->prepare('UPDATE World SET randomNumber = ? WHERE id = ?');
  19. // For each query, store the result set values in the response array
  20. while (0 < $query_count--) {
  21. $id = mt_rand(1, 10000);
  22. $randomNumber = mt_rand(1, 10000);
  23. $statement->execute( [$id] );
  24. // Store result in array.
  25. $world = ['id' => $id, 'randomNumber' => $statement->fetchColumn()];
  26. $world['randomNumber'] = $randomNumber;
  27. $updateStatement->execute([$randomNumber, $id]);
  28. $arr[] = $world;
  29. }
  30. // Use the PHP standard JSON encoder.
  31. // http://www.php.net/manual/en/function.json-encode.php
  32. echo json_encode($arr);