Jenkins & Wagner/Dreamweaver CS5 FD

9
Sue Jenkins Richard Wagner 9 IN 1 BOOKS BOOKS • Getting Started • Mastering the Basics • Working Like the Pros • Energizing Your Site • Publishing Your Site • Working Collaboratively • Building Web Applications • Making Pages Dynamic • Developing Applications Rapidly Dreamweaver ® CS5 ALL-IN-ONE Making Everything Easier! Click here to buy the book Sample Chapter

description

Sample Chapter

Transcript of Jenkins & Wagner/Dreamweaver CS5 FD

Page 1: Jenkins & Wagner/Dreamweaver CS5 FD

Sue JenkinsRichard Wagner

9 IN 1BOOKSBOOKS

• Getting Started• Mastering the Basics• Working Like the Pros• Energizing Your Site• Publishing Your Site• Working Collaboratively• Building Web Applications• Making Pages Dynamic• Developing Applications Rapidly

Dreamweaver® CS5A L L - I N - O N E

Making Everything Easier!™

Click here to buy the book

Sample Chapter

Page 2: Jenkins & Wagner/Dreamweaver CS5 FD

Chapter 2: Configuring MySQL Database Connections for PHP

In This Chapter✓ Connecting to a MySQL database

✓ Adding a database connection

✓ Editing or deleting a database connection

If you’ve decided to go the open-source route and use PHP as your appli-cation server platform and MySQL for your database, this chapter is for

you. (If you haven’t set up PHP, see Book VII, Chapter 1 for details.) You’ll find the discussion helpful whether you’re working with a Web-hosted remote server or running PHP and MySQL on your local machine.

In this chapter, we show you how to connect to your server and create a dynamic Web page with PHP in Dreamweaver CS5. Additionally, you find out how to set up a MySQL database connection and add, edit, or delete a connection.

However, before you begin, make sure that you have a Dreamweaver site created or opened that has server settings configured and operational. Dreamweaver needs this info to connect to the database.

Adding a PHP Database ConnectionTo create a database connection in PHP to access a MySQL database, you need to create a new dynamic PHP page. The database connection enables Dreamweaver to interact with the database using database connections through PHP. Dreamweaver automatically adds code specific to your dynamic page type to your new dynamic page when creating a database connection.

Creating a new dynamic PHP pageTo create a new dynamic PHP page in Dreamweaver, follow these steps:

1. Choose File➪New.

The New Document window appears, as shown in Figure 2-1.

40_610770-bk07ch02.indd 617 5/6/10 1:18 PM

Click here to buy the book

Page 3: Jenkins & Wagner/Dreamweaver CS5 FD

618

Figure 2-1: Create a new document.

2. Click the PHP icon in the Page Type list.

3. (Optional) Choose a Layout in the list.

For now, feel free to leave <none> selected in the Layout column.

4. Select XHTML 1.0 Transition in the DocType drop-down list.

5. Click Create.

A new dynamic PHP page is created, as seen in Figure 2-2.

Figure 2-2: A new PHP document is ready to go.

Adding a PHP Database Connection

40_610770-bk07ch02.indd 618 5/6/10 1:18 PM

Click here to buy the book

Page 4: Jenkins & Wagner/Dreamweaver CS5 FD

Book VIIChapter 2

Configuring M

ySQL Database Connections for PHP

619

Creating the database connection for PHPAfter you create a new dynamic PHP page (as described in the preceding steps), you can connect to the database. We show you how to connect to an existing MySQL database in this section. However, if you’d like info on how to get started with MySQL databases, check out www.mysql.org.

Connect to an existing MySQL database by following these steps:

1. Choose Window➪Databases.

The Databases panel appears. If you have a site open and a testing server configured, the first three steps are selected, as shown in Figure 2-3.

2. Click the plus (+) icon and select MySQL Connection from the drop-down list that appears.

The MySQL Connection dialog box appears, as shown in Figure 2-4.

3. Enter a name for the database connection in the Connection Name field.

Dreamweaver uses this name to reference your database connection. The name can contain only letters, numbers, and underscores.

Figure 2-3: The Databases panel before defining a database connection.

Adding a PHP Database Connection

40_610770-bk07ch02.indd 619 5/6/10 1:18 PM

Click here to buy the book

Page 5: Jenkins & Wagner/Dreamweaver CS5 FD

620

Figure 2-4: The MySQL Connection dialog box.

4. Enter the host name or IP address of the database server in the MySQL Server field.

This may be a host name or IP address.

If the MySQL server resides on the same system as the Web or applica-tion server, reference it locally with the host name localhost. This value applies even if you’re connecting to a remote server.

5. Enter the username for the database in the User Name field.

MySQL enables you to create users and assign permissions in its Web-based control panel. See your MySQL control panel if you need to set up a user.

6. Enter the corresponding password in the Password field.

7. Click the Select button.

If your settings are correct to this point, the Select Database dialog box appears with available databases, as shown in Figure 2-5.

If you are using a Web hosting provider and receive an error, check to make sure that MySQL is configured to allow external IP connections. The exact instructions will depend on your particular Web host.

Figure 2-5: The Select Database dialog box.

Adding a PHP Database Connection

40_610770-bk07ch02.indd 620 5/6/10 1:18 PM

Click here to buy the book

Page 6: Jenkins & Wagner/Dreamweaver CS5 FD

Book VIIChapter 2

Configuring M

ySQL Database Connections for PHP

621

8. Select your database from the list, and then click OK.

The Select Database dialog box closes, and the name of the database you selected appears in the Database field of the MySQL Connection dialog box.

9. Click the Test button to confirm that your connection is set up properly.

An alert box tells you whether the connection was successful.

10. Click OK to close the alert box.

11. Click OK to close the MySQL Connection dialog box.

Dreamweaver creates the database connection (see Figure 2-6).

You can then click down on the outline structure to display Stored Procedures, Tables, and Views (see Figure 2-7).

Figure 2-6: A MySQL connection has been established.

Figure 2-7: Working with a MySQL connection.

Adding a PHP Database Connection

40_610770-bk07ch02.indd 621 5/6/10 1:18 PM

Click here to buy the book

Page 7: Jenkins & Wagner/Dreamweaver CS5 FD

622

Editing a PHP Database ConnectionIn case you change the password for your database connection or the address of your database server changes, you need to know how to update your MySQL database connection settings.

To edit a MySQL database connection in PHP, follow these steps:

1. Open the Databases panel by clicking the Databases tab of the Application panel.

2. Right-click the database connection you want to edit.

3. Select Edit Connection.

The MySQL Connection dialog box appears.

4. Edit the connection properties as necessary.

5. Click the Test button to confirm that your connection is properly set up.

An alert box tells you whether the connection was successful.

6. Click OK to close the alert box.

7. Click OK to close the MySQL Connection dialog box.

Dreamweaver saves your changes to the database connection.

Deleting a PHP Database ConnectionTo delete a MySQL database connection in PHP, follow these steps:

1. Open the Databases panel by clicking the Databases tab of the Application panel.

2. Right-click the database connection you want to delete.

A list of options appears.

When you have a .php file open in the Dreamweaver editor, you have full access to PHP code hinting. To access code hinting, press Ctrl+Space (Windows) or Ô+Space

(Mac OS X). A context-sensitive drop-down list of functions and operations is displayed. When you select an item, help is displayed below the item.

PHP code hinting

Editing a PHP Database Connection

40_610770-bk07ch02.indd 622 5/6/10 1:18 PM

Click here to buy the book

Page 8: Jenkins & Wagner/Dreamweaver CS5 FD

Book VIIChapter 2

Configuring M

ySQL Database Connections for PHP

623

3. Choose Delete Connection.

An alert box appears to confirm your deletion.

4. Click Yes.

The database connection is deleted and removed from the list of con-nections on the Databases tab in the Application panel.

Deleting the database connection deletes a PHP file that Dreamweaver cre-ated in a Connections directory in your local directory. The directory itself remains. You can delete this directory if you want to keep your directory uncluttered.

Deleting a PHP Database Connection

40_610770-bk07ch02.indd 623 5/6/10 1:18 PM

Click here to buy the book

Page 9: Jenkins & Wagner/Dreamweaver CS5 FD

624 Book VII: Building Web Applications

40_610770-bk07ch02.indd 624 5/6/10 1:18 PM

Click here to buy the book