I normally create a functions.php file for all my websites, which is then called into every page. One of the first things that this functions file does, is connect to the appropriate database for the website.
Now and then, however, you need to manually call a database connection, before the functions file is loaded, for example, if you’re testing a condition and redirecting a user using the header() command in PHP.
The quickest way I’ve found to create your PHP/MySQL interaction is as follows:
1 2 3 | // CONNECT TO DATABASE mysql_connect("localhost", "dbname_username", "password") or die(mysql_error()); mysql_select_db("dbname_tablename") or die(mysql_error()); |
Obviously, you’d swap out the username, password and table name in the above, but it’s a handy snippet of code to have near to hand.

Thomas Read
February 11th, 2010 at 20:46
Thanks for writing about this. There’s a heap of good information on the www. You got a lot of good info here on your web site. I’m impressed – I try to keep a couple blogs pretty on-going, but it’s a struggle sometimes.
how to fix blue screen
February 11th, 2010 at 20:56
wow, awesome post, I was wondering
theoneandonlytruzer
February 14th, 2010 at 14:16
I have needed this information for so long. Where have you been!!! Thanks