Galery: sql used to populate the db + galery page refactor