Is it a good idea to store the username and password used to connect to a database in another file instead of the same .php file that connects to that database?
Yes, in a file above/outside of the document root directory (your web directory, basically).
What are the risks of not doing so?
If for some reason the PHP interpreter fails to parse your PHP script and the PHP source code is displayed to the client, your database account credentials would be displayed to the user.