Browse Source

Few suggestions for dbraw.php

- use bindParam instead of bindValue
- fetchColumn instead of fetch the entire row
- no need for a for loop, you have a count just count down
- swap empty with isset
- switch " to '
John Piasetzki 12 years ago
parent
commit
a0626028b6
1 changed files with 8 additions and 12 deletions
  1. 8 12
      php/dbraw.php

+ 8 - 12
php/dbraw.php

@@ -11,29 +11,25 @@ $pdo = new PDO('mysql:host=localhost;dbname=hello_world', 'benchmarkdbuser', 'be
 
 // Read number of queries to run from URL parameter
 $query_count = 1;
-if (!empty($_GET)) {
-  $query_count = $_GET["queries"];
+if (TRUE === isset($_GET['queries'])) {
+  $query_count = $_GET['queries'];
 }
 
 // Create an array with the response string.
 $arr = array();
+$id = mt_rand(1, 10000);
 
 // Define query
-$statement = $pdo->prepare("SELECT * FROM World WHERE id = :id");
+$statement = $pdo->prepare('SELECT randomNumber FROM World WHERE id = :id');
+$statement->bindParam(':id', $id, PDO::PARAM_INT);
 
 // For each query, store the result set values in the response array
-for ($i = 0; $i < $query_count; $i++) {
-  // Choose a random row
-  // http://www.php.net/mt_rand
-  $id = mt_rand(1, 10000);
-
-  // Bind id to query
-  $statement->bindValue(':id', $id, PDO::PARAM_INT);
+while (0 < --$query_count) {
   $statement->execute();
-  $row = $statement->fetch(PDO::FETCH_ASSOC);
   
   // Store result in array.
-  $arr[] = array("id" => $id, "randomNumber" => $row['randomNumber']);
+  $arr[] = array('id' => $id, 'randomNumber' => $statement->fetchColumn());
+  $id = mt_rand(1, 10000);
 }
 
 // Use the PHP standard JSON encoder.