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>
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;
}