Connect to DatabaseIn a previous tutorial, you learned how to create a customized config.php3 file which contained four pieces of information (name of your host server, your username, your password and the name of your database). Remember that we initialized the $host, $username, $password and $name_of_database variables in this config.php3 file. You have probably noticed that the config.php3 file is called into the program at the top of the ActivityEntry.php3 script with the require_once statement.
Now that we have those variables initialized and feed into the program, we need to identify our ActivityEntry.php3 script to the MySQL server. You learned how to do this with the @mysql_connect( ) function in another previous tutorial. We used the variables from the config file in the @mysql_connect statement.
Now we will add something new. We will add another function statement that will tell the MySQL server which database we wish to use. This second statement is the mysql_select_db( ) function.
As you can see this statement uses two variables ($name_of_database and $connection). We initialized the $name_of_database variable to "calendar" in the config.php3 file. The $connection variable was created in the mysql_connect( ) statement above it. Also, we reused some of the things you learned in the previous tutorial. The mysql_select_db( ) function uses the @ to suppress the default error message and the die( ) statement to customized an error message for the possibility that we cannot connect to the database.
Take a look at the finished changes to the ActivityEntry.php3 script. Now we are ready to work on the INSERT statement in the next tutorial.
Note--The purpose of this tutorial is to build a Personal Activities Calendar Program for personal use and teach the basics of PHP and MySQL databases. I have not included any security features such as preventing malicious input data. That would make this tutorial too complicated for the novice. If you were creating a program for the web, you would want to include such security in your program code.
A Custom Configuration File For PHP Programs
How to Connect to the MySQL Server with the PHP mysql_connect Function
The PHP Strtotime Function