Modif and tested code for retrieving data from db.