• Create BookmarkCreate Bookmark
  • Create Note or TagCreate Note or Tag
  • PrintPrint
Share this Page URL

Chapter 30. Building a Basic PHP Site > Setting Up a Database Connection

Setting Up a Database Connection

In PHP terms, the database connection is a script that calls on the driver to talk to the database. Dreamweaver creates this script for you and stores it in a special connections file when you choose data source name (DSN) from the Databases panel. Because this information gets stored in a special file that can be accessed by any PHP page in your site, you have to define the connection only once for the entire site.

Exercise 30.2 Creating a Database Connection

In this exercise, you create the connection script that will allow your pages to communicate with the antiques database. You must already have installed your database and created a driver for it (as outlined in the preceding section) before continuing with this exercise.

Because Dreamweaver has to know what kind of connection to create, you must have a dynamic document open before you can create the connection. From your local site, open catalog.php.

From the Application panel group, open the Databases panel. If you have catalog.php open, the panel will have a plus (+) button at the top. Click it and, from the pop-up menu, choose MySQL connection. Figure 30.7 shows this happening.

Figure 30.7. Choosing a MySQL connection from the Databases panel.


Is you’re used to using Dreamweaver with ASP or any other server technology, you might be wondering how you can make a connection before you’ve defined a database driver. For this kind of page, you don’t need one!

The MySQL Connection dialog box appears (see Figure 30.8). Several pieces of information are required to fill in this dialog box. For your connection’s name, enter antiques_conn. (The connection name will be used in the connection script. It can be any one-word name with no special characters, but it’s common practice to include con or conn in the name to denote a connection.)

Figure 30.8. Defining the antiques_conn database connection.

The MySQL server is the name or IP address of the computer housing the MySQL DBMS. If you’re set up for local development (with the server and MySQL on your working computer), enter localhost in this field. If you’re working on a remote computer, enter the IP address of the computer where MySQL and your web server reside.

Depending on how you set up your MySQL database, you might not think you have a username, but you do. If you can start up the MySQL client program without specifying a username, then your username is the username that you use to log onto your computer. If you don’t need a password to start MySQL, leave the password field blank. You can’t leave the username field blank.

The database field should contain the name of the database you want to connect to—in this case, antiques. You can type the name in the input field, but it’s a good idea to avoid typos and test your connection by clicking the Choose button. When you do this, if you’ve entered the above information correctly a dialog box will appear listing all the MySQL databases on the specified host (for example, localhost or the IP address you entered). If some of your information is wrong, or if your MySQL server program isn’t currently running, you’ll get an error message.

Finally, before you leave the dialog box, click Test. If your username or password is incorrect, you’ll get a permissions error. If everything is working properly, you’ll get a successful connection message.

When you have made a successful test, click OK to close the dialog box.

The Databases panel will now contain an icon representing your connection. Congratulations, You can now use this panel to explore your database. Expand the connection icon to see Tables, Views, and Stored Procedures. The antiques database contains only tables. Expand the Tables icon all the way to see that the database contains two tables—stockitems and customers—and to see what columns (information fields) each table contains. You cannot see the records stored in the database from here, but you can examine its structure (see Figure 30.9).

Figure 30.9. The Databases panel showing the structure of the antiques database.

In the Site panel, examine your local root folder. You’ll see a new connections folder. Inside that folder is the antique_conn.php file. That file contains your connection script. Open that file and examine it in Code view. You’ll see the following connection script:
# FileName="Connection_antiques_conn.htm" 
# Type="MYSQL" 
# HTTP="true" 
$hostname_nr = "localhost"; 
$database_nr = "ANTIQUES"; 
$username_nr = "don"; 
$password_nr = "maxx"; 
$nr = mysql_pconnect($hostname_nr, $username_nr, $password_nr) or 

You don’t need to know what everything in there means. However, one important piece of syntax that you should get familiar with is the <?php...?> tags. All PHP code in an HTML document is contained within these tags. When the PHP module processes this page, it looks for these tags and executes all code inside them. All other code on the page is assumed to be regular HTML or client-side scripting and is just passed back to the browser.



Not a subscriber?

Start A Free Trial

  • Creative Edge
  • Create BookmarkCreate Bookmark
  • Create Note or TagCreate Note or Tag
  • PrintPrint