in reply to check if databse exists before connection is made!

If it doesn't exist you'll get undef for $db. Check the return value of your connect and do something appropriate.