1

hello there i am working on a function which should return a database result set AND the time it took the query to retrieve the data. Works good but i don't know how to return the time and the result set and make them available in the view. Thanks for help.

My code looks like this:

public function getNumResults($term) {

   /* Count query Execution */
   $starttime = microtime(true);

   $query = $this->con->prepare("SELECT COUNT(*) as total
       FROM sites WHERE title LIKE :term
       OR url LIKE :term
       OR keywords LIKE :term
       OR description LIKE :term");

   $endtime = microtime(true);

   /* Calculates total time taken */
   $duration = $endtime - $starttime;

   $searchTerm = "%". $term . "%";
   $query->bindParam(":term", $searchTerm);
   $query->execute();

   $row = $query->fetch(PDO::FETCH_ASSOC);

   return $row["total"];

}

In the view i return the result set like so:

<div class="mainResultSection">
        <?php
          $resultsProvider = new SearchResultsProvider($con);
          $numResults = $resultsProvider->getNumResults($term);
          /* Not working */
          $timeResults = $resultsProvider->getNumResults($term)->duration;
          echo "<p class='resultsCount'>$numResults results found. In $timeResults <p>";
        ?>
      </div>
  • 3
    Since you can only `return` one thing, chuck 'em both in an array and return the array. – CD001 Aug 24 '18 at 14:54
  • 1
    Your microtime does not account for the actual query phase. Also why not just make it a property on your search object? – mario Aug 24 '18 at 14:56
  • i thought about writing another method but i am not sure if this extends the time for the function to calculate the request so i end up with a longer time it used to be... –  Aug 24 '18 at 15:04
  • Did you give up??? – AbraCadaver Nov 29 '18 at 16:46
  • nooooo moving further @AbraCadaver –  Nov 29 '18 at 19:00

2 Answers2

0

You need to return an array. Maybe just add it to $row. Also, you need to get the start time before the query and get the end time after:

   //other code

   $starttime = microtime(true);
   $query->execute();
   $endtime = microtime(true);
   $row = $query->fetch(PDO::FETCH_ASSOC);
   $row["duration"] = $endtime - $starttime;

   return $row;

Or for your current code you can just add it to an object variable:

   $this->duration = $endtime - $starttime;

Then access with:

   $timeResults = $resultsProvider->duration;
AbraCadaver
  • 78,200
  • 7
  • 66
  • 87
0

Too inexperienced. I went with another function to get the duration. But the other answers given helped a lot. Thanks, here the final code working:

/* Time to get results */
public function getTimeResult($term){
   $query = $this->con->prepare("SELECT COUNT(*) as total
        FROM sites WHERE title LIKE :term
        OR url LIKE :term
        OR keywords LIKE :term
        OR description LIKE :term"
   );

   $searchTerm = "%". $term . "%";
   $query->bindParam(":term", $searchTerm);

   $starttime = microtime(true);
   $query->execute();
   $endtime = microtime(true);

   /* Calculates total time taken */
   $duration = $endtime - $starttime;

   return $duration;
}