I am trying to display the results of an SQL search in multiple places, but I can only seem to get it to print once (the first time it is called).
Here is what I have:
$stmt = $connection->prepare("SELECT DISTINCT genre FROM movies");
$stmt->execute();
$results = $stmt->get_result();
while ($row = mysqli_fetch_array($results)){
print ($row['genre'] . "<br>");
}
It makes sense to me that doing this "uses up" all of the results, so if I were to call the while again, there would be nothing, and therefore it would print nothing.
So, is it possible to copy the results to another variable for later use? I have tried:
$results2 = $results;
$results2 = array();
$results2 = $results;
$results2 = new stdClass();
$results2 = $results;
But I keep getting the same result... and empty array after the first call.
Do I just need to call the SQL query again? It seems like there's gotta be a better way.