You have an error in your SQL syntax near 'Resource id #12' at line 1
So where in your code is 'Resource id #12'?
Is 'Resource id #12' being fed into the sql as a query value? only (and don't shoot me if I'm mistaken on this cos its late and I'm tired) the hash is a special character and would need to be escaped.
The code you've posted doesn't really help us shed any light on your true problem.
Mind you, you do have a mysql_query() call inside another mysql_query() call which could be the real root of the problem.
$query_out = mysql_query("SELECT imagine FROM felicitari WHERE random='$random' and numele='$numele'");
mysql_query($query_out) or
die (mysql_error());
$query_out actually performs the query first time round, so adding the "mysql_query($query_out) or" is asking for trouble.