Connecting to a Database with PHP

page 1: Introduction
page 2: Connecting to the Database
page 3: Displaying Tables
page 4: Inserting New Records
page 5: Updating Records
page 6: Deleting Records

Connecting to the server and a particular database

The first thing we have to do is connect to the MySQL server, then select the database we want to use. This is accomplished by the use of functions, or pre-built mini programs that are a part of the PHP scripting language. We are going to use three functions: mysql_connect, mysql_select_db, and mysql_close.

Before we start our "mini programs" we're going to have to add the PHP delimiters that declare that we are presenting a block of PHP code. <?php begins the script, and ?> ends it. We can go back and forth between PHP and HTML as long as we always remember to surround the PHP scripts with these delimiters. In the case of the script below, we're going to place it between the opening body tag and the closing body tag.

Next we are going to set up a variable, which is like an imaginary box in which to store specific values. PHP variables start with a dollar sign ($), and can be numeric or strings (of characters.) See our first variable? When we connect to the MySQL server, a positive integer is returned which labels the connection. $connectID is the name I gave this variable, but I could have named it $linkID or $labelID or anything meaningful to me.

The mysql_connect function takes three "arguments." The first is the name or IP address of the host computer. If you are using your own Web server, you can either type in "localhost" or "127.0.0.1." The second argument is the user name. If you set up MySQL, you may be using it under the Super User or "root." Finally, we need the password of root in the MySQL server. When you installed MySQL, you should have set up a password as a security precaution.

The second function, the mysql_select_db will allow us to choose which database we would like to work with. In this case, we could use the animal_rescue database we created in a previous article. We're also going to designate that connection we are using.

You may notice what we call a conditional statement. If and else can tell the script to perform one task if a certain condition is true, but another if it is not. So all we're doing here is telling the browser to present on the screen one statement if we successfully connect to the database, but another if we do not. != is the operator that tells us something is not equal to something else.

Finally, we are going to explicitly close the connection to the database with the mysql_close function. Again, this function takes the argument (within parentheses) that tells us which connection we are closing.

<body>
<?php
$connectID = mysql_connect("localhost", "root", "mypasswordhere");
if (mysql_select_db("animal_rescue", $connectID) !=FALSE)
{
print "Animal Rescue was successfully selected.";
}
else
{
print "The connection to animal_rescue failed.";
}
mysql_close($connectID);
?>
</body>

So there's your first script. If you have this database in the MySQL server, try it out. Otherwise, enter the name for some other database you have. Now wouldn't it be nice to actually be able to see one of our tables in the browser? Let's look at the adoption_list table now-->

Back to Unix Tips Page-->