Also, I changed some % to %%. Although this wasn't a problem in practice, using a single % means relying on internal behavior of db_query.