Fixed the database query functionality