Code duplication. Only $sql is different?