g Easier! Making Everythin
™
5 S C r e v a e w m a e Dr ®
N E A L L- I N - O
91 BOOKS IN
• 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
Sue Jenkins Richard Wagner Click here to buy the book
Sampleer Chapt
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
I
f you’ve decided to go the open-source route and use PHP as your application 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 Connection To 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 page To 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.
Click here to buy the book
618
Adding a PHP Database Connection
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.
Click here to buy the book
Adding a PHP Database Connection
619
Creating the database connection for PHP After 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â&#x20AC;&#x2122;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â&#x17E;Ş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 dropdown 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. Book VII Chapter 2
Configuring MySQL Database Connections for PHP
Figure 2-3: The Databases panel before defining a database connection.
Click here to buy the book
620
Adding a PHP Database Connection
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 application server, reference it locally with the host name localhost. This value applies even if youâ&#x20AC;&#x2122;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 Webbased 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.
Click here to buy the book
Adding a PHP Database Connection
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).
Book VII Chapter 2
Configuring MySQL Database Connections for PHP
Figure 2-6: A MySQL connection has been established.
Figure 2-7: Working with a MySQL connection.
Click here to buy the book
622
Editing a PHP Database Connection
PHP code hinting 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 Ă&#x201D;+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.
Editing a PHP Database Connection In 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 Connection To 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.
Click here to buy the book
Deleting a PHP Database Connection
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 connections on the Databases tab in the Application panel. Deleting the database connection deletes a PHP file that Dreamweaver created 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.
Book VII Chapter 2
Configuring MySQL Database Connections for PHP
Click here to buy the book
624
Book VII: Building Web Applications
Click here to buy the book