To select records from multiple tables, you need to join them—not in the literal sense but by using SQL commands that tell the database you want to retrieve results from more than one table. We’ll look in more detail at the basic SQL commands shortly, but first let’s try it out for real by displaying quotations and their associated authors from the authorsand quotationstables.
The “Stroll Along the Thames” page you’ve used in several chapters has a pull quote with a quotation from Samuel Johnson. In this exercise, you’ll replace that static quotation with one drawn at random from the authorsand quotationstables. This demonstrates three useful techniques: how to join multiple tables, randomize the order of recordset results, and limit the number of results. You can use an existing version of the page, as long as it has a .phpextension. However, you will probably find it easier to use the ver- sion in examples/ch16, because it contains no other PHP script, so you can see the new code in isolation.
1.Copy stroll_quote_start.phpfrom examples/ch16, and save it as stroll_quote.php in workfiles/ch16. Click Update if Dreamweaver prompts you to update links in the page.
2.Click the plus button in the Server Behaviorspanel, and select Recordset from the menu. Because you’ll be selecting columns from more than one table, you need to use the Recordset dialog box in Advanced mode (see Figure 16-7). If necessary, click the Advanced button on the right of the dialog box to switch from Simple mode.
3.Your recordset should have a meaningful name, so type getQuotein the Namefield.
4.The recordset will be used in a public page, so choose the nonadministrative user account for Connection. If you’re using the same connections as me, select connQuery. If you have only one user account, use connAdminas before.
If Dreamweaver inserted a SELECTquery in the SQLfield when you switched from Simple mode, clear the field by selecting any code and pressing Delete. The Recordsetdialog box should now look like Figure 16-8.
The SQLfield in the top half of the dialog box is where you build the query that will be sent to the database. If you’re familiar with SQL, you can type your query here manually, but the Database itemsfield takes a lot of the hard work out of typ- ing. It also reduces the likelihood of spelling mistakes.
Displaying a random quotation
Figure 16-8.The Advanced mode of the Recordset dialog box lets you create more complex SQL queries.
5.In the Database items field, expand Tables. You should now see the authors, quotations, and users tables listed. Expand quotations, highlight quotation, and click the SELECTbutton, as shown here:
This starts building the SQL query. You should now see this code in the SQLfield:
SELECT quotations.quotation FROM quotations
6.Expand authorsin the Database itemsarea, and highlight first_name. Click SELECT. 7.Highlight family_name, and click SELECT. The SQL query should now look like this:
SELECT quotations.quotation, authors.first_name, authors.family_name FROM quotations, authors
16
8.If you click Testnow, you will see the first quotation attributed everyone listed in the authorstable, starting with Woody Allen, Matsuo Basho, and so on. Then the second quotation attributed to each author. The Dreamweaver test shows only the first 100 results, but if you run the same query in phpMyAdmin, you’ll see there are 2,000 results altogether—every record in the quotationstable has been matched with every record in the authorstable. In other words, it produces every possible combination.
You have just joined two tables but not in a very practical way.
9.To get the result you want, you need to add a WHEREclause that matches the for- eign key in the quotationstable to the primary key in the authorstable. Highlight author_idin the quotationstree in Database items, and click the WHEREbutton. This adds WHERE quotations.author_idto the end of the SQL.
10.Expand the authorstree in Database items, and highlight the other author_id. Click WHEREagain. Each time you click WHERE, Dreamweaver always adds whichever column is highlighted using AND, so the final line of the SQL query will now look like this:
WHERE quotations.author_id AND authors.author_id
Although ANDis often what you want in a WHEREexpression, it’s not always the right choice, so you have to replace it manually. Click inside the SQLfield, and replace ANDwith =. The SQL should now look like this:
11.Click the Test button now, and you’ll see that each quotation has now been cor- rectly matched with the right name. Adding the WHEREclause uses the foreign key to select only those records where author_idmatches in both tables. Click OK to close the Test SQL Statementpanel.
12.If you click Testagain, the recordset appears in exactly the same order, which is the order the quotations were entered into the table. To change the order, select family_namein the authorstree in Database items, and click the ORDER BYbutton.
The SQL query should now look like this:
SELECT quotations.quotation, authors.first_name, authors.family_name FROM quotations, authors
WHERE quotations.author_id = authors.author_id ORDER BY authors.family_name
13.Click the Test button again. The quotations should be ordered according to family name.
14.Close the test panel, and add DESCat the end of the final line of the SQL query like this:
ORDER BY authors.family_name DESC
When you test the query this time, a quotation from Wordsworth will be at the top of the list, with the authors listed in reverse alphabetical order (DESC stands for
“descending”).
15.You want to display a random quotation in the page, so edit the last line of the SQL query like this:
ORDER BY RAND()
This uses the MySQL function RAND()to generate a random order. Make sure there is no space betweenRANDand the parentheses.
16.Since you need only one quotation to display in the page, it’s inefficient to create a full recordset, so let’s limit the result to just one record. How do you do that?
Easy—change the final line of the SQL query like this:
ORDER BY RAND() LIMIT 1
17.Use the test panel several times to make sure you’re getting just one random quo- tation and the associated names. Once you’re happy that everything is as expected, click OKto close the Recordsetdialog box.
18.In Design view, highlight the quotation from Samuel Johnson, open the Bindings panel, select quotation from Recordset (getQuote), and click Insert. Then replace Samuel Johnson’s name and the date with dynamic text for first_name,family_name, and a space in between.
19.Save stroll_quote.php, and load it into a browser. Each time you click the browser’s reload button, you should see a quotation picked at random from the 50 in the quotationstable (see Figure 16-9). Occasionally, you’ll see the same quota- tion twice in succession, but that’s no different from rolling two 6s twice in succes- sion from a pair of dice.
You can check your code against stroll_quote.phpin examples/ch16.
Figure 16-9.The quotations and authors’ names are drawn seamlessly from separate tables.
16