Things You'll Need:
- a MySQL database
-
Step 1
Launch notepad or some other text writing software that you use to create your PHP files. I use notepad since it is free. A PHP file is nothing more than a text file.
-
Step 2
First connect to your database by writing the following line in your PHP file:
mysql_connect("localhost", {username}, {password}) or die(mysql_error());
Replace {username} and {password} with the username and password for your database. This says connect to MySQL with the {username} and {password} being provided on the machine called "localhost". If it fails return the error. -
Step 3
Now select the database you actually want to connect to with this code:
mysql_select_db({database name}) or die(mysql_error());
Replace {database name} with the actual name of your database. This line says to connect to the database you have provided with the credentials you submitted in the previous step. If it does not connect, return the error being reported.









Comments
mpodlesny said
on 6/22/2009 thanks...it only looks complicated..:)
kimmij said
on 6/22/2009 I will never be as smart as you about this stuff. Impressive! Great article on How to Connect to a MySQL Database with PHP. 5 *'s and recommended.