Bringing In The Database
Bringing In The Database
One of the coolest features of Dreamweaver MX is the ability to build a dynamic database-driven Web site without having to write a single line of PHP code on your own. Over the next few pages I’m going to demonstrate how, by having Dreamweaver do the hard work of writing PHP code that allows you to view, add and delete records from a MySQL table.
Let’s assume that I have a database table called “user”, as shown below.
mysql> SELECT * FROM user;
| id | username | password |
| 3 | merrill | 2fda14e52bde1a87 |
| 4 | pooja | 0d53926314545f3c |
| 5 | john | 2ca0ede551581d29 |
| 6 | joe | 7b57f28428847751 |
| 7 | tom | 675bd1463e544441 |
| 8 | bill | 656d52cb5d0c13cb |
6 rows in set (0.05 sec)
Now, the built-in features of Dreamweaver MX allow you to automate the processes of listing the records in the table, adding new records, modifying existing records, and deleting records – all with minimum hand coding.
The first thing to be handled is the database connection. Dreamweaver MX does the dirty work of opening, managing and closing the database connection – all you need to do is pop open the “Application” panel (on the right side of the workspace), click the “Database” tab, hit the button with a plus (+) symbol on it, and select “MySQL Connection” from the option list.
You should see the following dialog:
This box allows you to specify access information for the database server. For this example, create a connection called “dbConnect” and provide Dreamweaver with the username and password required to access the database server, together with the name of the database to be used.
Once the connection has been created, Dreamweaver will automatically add a set of scripts to your site directory; these will be used for all subsequent database interaction. If the connection is created successfully, you should be able to view all the tables (as well as views and stored procedures for compliant databases) in the “Database” tab of the “Application” panel.
With the database connection created, the next step is to write a script that will list all the records in the table. Use the “Bindings” tab of the “Application” panel to create a “Recordset” object (click the button with the plus symbol again to get to this option) and you’ll get a simple wizard-like interface which allows you to retrieve a set of records from the database.
Once you’ve filled in the requisite information – note that you can also sort and filter the results – hit the “Test” button to see if everything works as it should. If it does, save your settings and you’ll now see a new item in the “Binding” tab of the “Application” panel, referencing your newly created Recordset.
You can customize the SQL query created via the “Advanced…” button in this wizard.