FireFly, thank you so very much! I've used the substr method. Just chopping off the end was by far the simplest solution.
I was pretty sure ahead of time no method of counting the number of results in a query (like with $DB_site->num_rows()) woud work, because the number of results in the last query was further reduced by the if ($getperms[canview]) part. In other words, not all of the results from the query would end up being used, so in all likelihood, the highest number would never be reached.