updateraw.php 1.4 KB

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