[ticket/12012] Remove duplicated code (only the $sql are different)

PHPBB3-12012