g
Printer Friendly Version

editor  
BellaOnline's PHP Editor
 

Personal Activities Calendar Program Use the strtotime and mysql_select_db Functions - 2

Connect to Database

In 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.

require_once('config.php3');

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.

$connection =
@mysql_connect("$host", "$username", "$password")
or die ("Could not connect to the database
at this time. Please try later.");
Note--The arrow indicates that the code is wrapped to a second line and should really be all on one line.

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.

$db =
@mysql_select_db($name_of_database, $connection)
or die("Could not connect to the database
at this time. Please try later.");
Note--The arrow indicates that the code is wrapped to a second line and should really be all on one line.

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.

← Back




      A Custom Configuration File For PHP Programs
      How to Connect to the MySQL Server with the PHP mysql_connect Function

      The PHP Strtotime Function

This site needs an editor - click to learn more!

PHP Site @ BellaOnline
View This Article in Regular Layout

Content copyright © 2013 by Diane Cipollo. All rights reserved.
This content was written by Diane Cipollo. If you wish to use this content in any manner, you need written permission. Contact Editor Wanted for details.



| About BellaOnline | Privacy Policy | Advertising | Become an Editor |
Website copyright © 2013 Minerva WebWorks LLC. All rights reserved.


BellaOnline Editor