Fetching and formatting content from a database
I would make a single file with the MySQL server connection, so you can use the same connection in other functions, etc..

Also you should pass string variables directly.

$stmt-&gt;prepare("SELECT COUNT(*) AS count FROM public  $selection");


Did you know, that you can check every single called function if a error occurred?
For example:

if(!($stmt-&gt;execute()) {
   die('Unable to execute!'. $mysqli-&gt;error);
}


The next point is, why do you execute another query to count the result rows?
In the mysqli_stmt object $num_rows. http://php.net/manual/en/mysqli-stmt.num-rows.php