in reply to substitutions with unknown data

I think what's happening is that you're exhausting your $sth->fetch on the first input line, so that in subsequent lines, the while ($sth->fetch) loop doesn't even execute. What I think you have to do is slurp all your DB records into some array @emoticons (or whatever you perfer), and then replace the while ($sth->fetch) with

for my $emoticon (@emoticons) { my ($id, $name, $location, $face) = @$emoticon; # do your substitution, with the fixes already given # to you in other comments }

the lowliest monk