[ticket/13205] Explicitly cast ids to integers and reformat SQL

PHPBB3-13205