Easy-to-follow video tutorials help you learn software, creative, and business skills.Become a member
In this chapter of the video series, I am going to describe features of Dreamweaver CS5 that allow you to easily connect to a MySQL database and retrieve and display data with generated PHP code. I'll be working in a version of this site in the exercise files, and if you have access to the exercise files, you can follow along. I'll go to the menu and choose Site > Manage Sites. I'll Edit my site, and then I'll set my local site folder to the folder 05_bindingandbehaviors, under the exercise files.
In previous chapters, I switched to a new site for each exercise. In this chapter, all of the exercises will be done in a single set of files, but I'll use a new file for each exercise, so I'll only need to change the site once for the entire chapter. I'll click Select and then click Save, and then click Done, and if you see any other messages along the way, click OK to clear them. The first step in connecting to your database is to define a database connection. You do this through the Databases panel.
You can get to this panel through the menu, by selecting Window > Databases, or you can press the associated keyboard shortcut, or if you already have the panel in your workspace, you can go to it directly. I am working in the App Developer Plus workspace, and the Databases panel is docked in the right section. I'll open the Databases panel and then click the Plus button. If the Databases panel is disabled, check to make sure that you have a PHP file open and that Live View is turned off.
When you're working with PHP in Dreamweaver CS5, you're only allowed to connect to MySQL databases. So I'll choose the only available item, MySQL Connection. You can set the Connection name to anything you like. I typically set it to the same as the database name itself. So I'll set my Connection name as explorecalifornia. The MySQL server entry should be pointed at the location of your database server. I am working with a copy of MySQL on my local system, so I'll type in "localhost." If you're working with a copy of the database that's on another machine, you can provide either the DNS name or the IP address.
Next I'll select a User name. When you install WAMP server or MAMP, the default administrative username is root, and that's what I'll use. In a production environment, you'll have a unique username. The password will differ depending on whether you are working in MAMP on Mac or on WAMP server on Windows. If you're working on Mac OS X with MAMP type in the word "root," again, in all lowercase. If you're working on WAMP server on Windows, leave the password blank.
Now click the Select button. You should see a list of the available databases in your database server. Depending on whether you're working on MAMP or WAMP server, you'll see either 4 or 5 databases. Choose explorecalifornia and click OK. Then click Test. This step verifies that Dreamweaver can connect to your database. If you're working on a WAMP server with a blank password, you'll see this warning dialog. If you are working on MAMP, you won't. If you see this warning dialog, click OK and then click Test again.
And now, regardless of whether you're working on Mac or Windows, you should see the dialog, Connection was made successfully. Click OK and then click OK again. If you have any trouble with this part of the process, make sure that you can open phpMyAdmin and see the database from there, and then come back to Dreamweaver and try creating the connection again. When you define the connection, a file is automatically created in your site. It's placed in a folder named Connections, which you'll now find in your Files panel.
If you don't see the Connections folder, click the Refresh button in the Files panel's toolbar. Once you see the Connections folder, open it up, and you should see a file named explorecalifornia.php. Double-click on the icon next to the file name to open it, and then if it opens up in Design View, switch to Code View. This file works by setting a number of variables and then using a PHP function called mysql_pconnect. Those variables are passed in to create the connection.
The variable explorecalifornia represents the connection itself, and it will be used in any PHP pages throughout your web site, to connect to the database in order to retrieve or update data. The four variables are the hostname, which is set to localhost, the database explorecalifornia, the username root and the password, which will either be a blank value for Windows or the word "root" for Mac. To change the connection definitions, you can either edit this file directly, or if you prefer, you can go back to the Databases panel and double-click on the database, and that will reopen the MySQL Connection dialog.
You can make changes here and click OK, and the connection file will be recreated. So that's a look at how to define your database connection. You go through the Databases panel, provide all of the required parameters, and the resulting file is stored in the Connections folder, which is stored in the site root.
Get unlimited access to all courses for just $25/month.Become a member
82 Video lessons · 97366 Viewers
61 Video lessons · 84618 Viewers
71 Video lessons · 68815 Viewers
56 Video lessons · 101285 Viewers
Access exercise files from a button right under the course name.
Search within course videos and transcripts, and jump right to the results.
Remove icons showing you already watched videos if you want to start over.
Make the video wide, narrow, full-screen, or pop the player out of the page into its own window.
Click on text in the transcript to jump to that spot in the video. As the video plays, the relevant spot in the transcript will be highlighted.
Your file was successfully uploaded.