Aside from the already mentioned fact that you are trying to print $name outside of its scope, I see two potential problems: 1) NUM = '$num' ... numeric values shouldn't be quoted in SQL although your backend may or may not care (which backend, btw?) 2) You can't necessarily depend on the case of the keys in a fetchrow_hashref() unless you have previously declared FetchHashKeyName, see the DBI docs on FetchHashKeyName.
I have to say that $num inside of the while loop by my mistake shouln'd be there, it sould be any other name for the new value returned from the DB, like $new_name.