It shouldn't be too difficult. Just add a MySQLi recordset with the same query. Then loop on the result to create the array structure that your code uses. Really it would just be a matter of rewriting those couple of lines of code in your while loop.