Error message is wrong -- this will only fail if the query failed. If there are no directories in the DB then the below loop will just have 0 length.