Run a duplicate query doesn't work

I return the results of a query like

 <?php

 if (mysqli_num_rows($devices_on_rack) > 0) {

	while($devices_row = mysqli_fetch_assoc($devices_on_rack)) {
	//display results
	}
 } else {
 echo "0 results";
 }
 ?>

which is working great, however if I try to run it again

I get nothing.How can I get it to work?

You would need to perform a data seek in order to be able to fetch the result set again.

However, forget about that. You need to separate the different concerns in your code, which will make it easier to design, write, test, debug, and maintain your code and make it easier to change the data-source, such as switching to the PDO database extension. The database specific code should not be inside the html document. You should put the database specific code before the start of the html document, fetch all the data from the query into an appropriately named php variable, then test/loop over that variable inside the html document. For what you are currently asking about, you would just test/loop over that variable a second time.

1 Like

If you try to run what again? If you try to run the loop again, as @mabismad said, you need to seek to the start of the result set. If you try to run the query again, that would be a different thing.

1 Like