I don't know about a totally automated method, wait for the guru's to answer but...
I always use a class to access the database. This helps because
a) I have diff passwords in prod and dev but the class is static. Therefore my php scripts don't have to be changed when they are uploaded as they just call the class which exists
b) my php scripts are much easier to read because they just say $db->query($sql) and I can put my effort into making sure $sql is right.
and then you could add
c) each time I connect I run a standard script
Because each and every database access is using your class you know that the standard script is being run.