Retrieving information from the database

Một phần của tài liệu The Essential Guide to Dreamweaver CS4 with CSS, Ajax, and PHP phần 7 pps (Trang 58 - 61)

Inserting information into a database is fine, but there’s not much point unless you can retrieve it and do something useful with it. Retrieving information from a database involves creating a SQL SELECTquery. As the name suggests, it selects information from the database according to your criteria and returns the results. Dreamweaver calls this a recordset. Once you have created a recordset, you can use it to display the results of the query in a web page. Although there’s currently only one record in the userstable, let’s build a page to display a list of all registered users, because this is an essential prerequisite to being able to update and delete information stored in the database.

These instructions show you how to use the Recordsetdialog box in Simple mode to query the userstable in preparation for displaying the results in a web page.

1.Open list_users.php, and give it a title and heading, such as Registered Users. Insert a link to register_user.phpand a table with two rows and three columns. I made the table 500 pixels wide, with no border, cellpadding, or cellspacing. I also set Headerto Top.

Type Name,Username, and Administratorin the first row. The page should look like this:

2.Open the Recordsetdialog box by clicking the plus button in the Server Behaviors panel and selecting Recordset. The Server Behaviorspanel is normally grouped with the Database and Bindings panels. If you can’t see it, select Window ➤ Server Behaviorsto open it, or press Ctrl+F9/Cmd+F9.

You can also click the Recordsetbutton on the Datatab of the Insertbar or select Insert ➤Data Objects➤Recordset.

The Recordsetdialog box has two modes: Simple and Advanced. If this is the first time you have opened the dialog box, it will be in Simple mode, as shown in Figure 14-12.

Creating a recordset

S T O R I N G U S E R R E C O R D S I N A D ATA B A S E

615

14

Figure 14-12.

The Recordset dialog box in Simple mode is used for basic SELECT queries.

You can tell which mode you’re in by looking at the buttons on the right side of the dialog box. If you’re in Simple mode, the fourth button is labeled Advanced; and if you’re in Advanced mode, it’s labeled Simple(because it switches to the opposite mode).

3.By default, Dreamweaver enters a generic value such as Recordset1, Recordset2, and so on, in the Namefield. However, the name is used to create several PHP vari- ables, so it’s better to choose something that tells you what the recordset is for.

Use only letters, numbers, and the underscore. Don’t use any spaces. Some people use the convention of beginning recordset names with rs, but this isn’t necessary.

The name I have chosen is listUsers.

4.Dreamweaver CS4 now remembers the most recent connection you used, so the connAdminconnection is automatically selected. Although this recordset performs only a SELECToperation, you’ll be editing the records later, so it’s more consistent to use the administrator connection for all the pages.

5.There’s only one table in the database at the moment, so the userstable is also selected automatically.

6.The Columnsfield has two radio buttons: Alland Selected. By default, the Allradio button is selected, and the columns are grayed out. A lot of beginners select All every time, even if they need only one or two columns. It’s easy, and it makes the SQL query a lot easier to read (we’ll study SQL syntax in Chapter 16). However, it’s a bad habit. Even if you need all columns, it’s considered best practice to select them individually because it makes the meaning of your code much clearer.

Choose the Selectedradio button, and Ctrl-click/Cmd-click username, first_name, family_name, and admin_priv.

7.You can ignore the Filtersettings this time. I’ll explain their use later.

8.Although there’s only one record at the moment, it’s a good idea to decide how the results should be sorted when there are more records in the table. Open the drop-down menu labeled Sort. It lists all the columns in the table. Choose family_name. This enables the drop-down menu to the right. It has two options:

Ascendingand Descending. Select Ascending. This will sort all results by the family name in alphabetical order.

9.Click the Testbutton on the right of the Recordsetdialog box. This opens the Test SQL Statementpanel with the results of the query, as shown here:

In Simple mode, you can sort by only one column. In Chapter 16, I’ll show you how to use Advanced mode to sort by multiple columns.

10.Click OKto close the test panel, and then click OKagain to close the Recordsetdia- log box and create the recordset.

11.The listUsersrecordset should now be listed at the top of the Server Behaviors panel, as shown in the following screenshot:

12.Save list_users.php. Leave the page open ready to insert the code that will dis- play the results of the recordset. If you want to check your page so far, compare it with list_users_01.phpin examples/ch14.

Một phần của tài liệu The Essential Guide to Dreamweaver CS4 with CSS, Ajax, and PHP phần 7 pps (Trang 58 - 61)

Tải bản đầy đủ (PDF)

(94 trang)