Use prepared query
Add total_matches variable instead of function