Using wildcard characters in a search

Một phần của tài liệu The Essential Guide to Dreamweaver CS4 with CSS, Ajax, and PHP phần 9 ppsx (Trang 20 - 24)

In SQL, the equal sign looks only for an exact match. All the examples so far have used the authors table, where each column normally contains only a single word. A search for

“William” produces two results: William Shakespeare and William Wordsworth. However, a search for “Will” produces no results. You might also want to search for all family names beginning with “S” or search the quotationstable for all entries that include “winter.”

When searching through columns that contain short text entries or numbers, you can use wildcard characters in your search. For longer sections of text, you should consider creat- ing a FULLTEXTindex, which I’ll describe later in this chapter.

MySQL has two wildcard characters: the underscore (_) matches a single character, and the percentage sign (%) matches any number of characters. A particularly useful feature about

% is that it also matches nothing. This means that a search for “Will%” matches both William and Will on its own. Consequently, most wildcard searches use %.

To use a wildcard character in a SQL query in Dreamweaver, add it to the beginning, end, or both ends of the runtime variable. Also, replace the equal sign with the keyword LIKE.

So, to search for authors based on the first part of their name, change the WHEREclause in find_author_09.phplike this:

WHERE first_name LIKE colname% AND family_name LIKE colname2%

You can test this in find_author_12.php. Start by entering the first part of a name in both fields. For example, if you type Win the First namefield and Sin the Family namefield, the result is William Shakespeare. Try it again, just typing Win the First namefield. You should see four results.

Pause a moment to think about this. The SQL query uses AND, so shouldn’t there be something in both fields? To understand what’s happened, repeat the test with find_author_13.php. The SQL query is identical, but the page displays the query along with the results, as shown in Figure 17-9.

Figure 17-9.Using AND with a wildcard character search allows a field to be left blank.

Although nothing is entered in the second field, the wildcard character %is added to the end of the runtime variable. This results in the second condition matching the family_namecolumn with %—in other words, anything.

Now try it with find_author_14.php, where the only difference is that AND has been changed to OR.

If you enter values in both fields, you’ll get the results that you expect. However, if you leave one of the fields blank, you’ll always get a full list of all records. This is because the query tells the database to match anything in one of the fields.

17

Adding %at the front of a runtime variable lets you search for words that end with a par- ticular letter or series of characters. Putting %at both ends of a runtime variable finds the search expression in the middle of a string; and since %can also match nothing, it means the search term can be anywhere—at the beginning, in the middle, at the end—or it can even be the full string itself.

So, let’s bring the quotationstable into our search.

This exercise adapts the SQL query used in quote_list.php in the previous chapter.

Instead of displaying a list of all quotations and their authors, it uses a runtime variable with %at both ends to search for quotations that contain a specific word or phrase. To save you time, I have created find_quote_01.phpin examples/ch17for you to use as a starting point. The finished code is in find_quote_02.php.

1.Copy find_quote_01.phpto workfiles/ch17, and open it in the Document win- dow. The page contains a form with a single text input field called searchTerm, a submit button, and code to display the results of the search.

2.Double-click Recordset (getQuote) in the Server Behaviors panel to open the Recordsetdialog box. The SQL query looks like this:

SELECT authors.first_name, authors.family_name, quotations.quotation FROM quotations LEFT JOIN authors USING (author_id)

ORDER BY authors.family_name

It’s based on the query in quote_list.phpin Chapter 16 (it doesn’t get quote_id and uses a simpler ORDER BYclause). Click the Test button, and you’ll see every quotation listed with its author’s name.

3.To search for quotations containing a particular word or phrase, you need to add the quotationcolumn to the WHEREclause. In the Database itemssection at the bot- tom of the Recordset dialog box, expand Tables, and highlight quotation in the quotationstree menu. Click the WHEREbutton to add it to the SQL query. The query should now look like this:

SELECT authors.first_name, authors.family_name, quotations.quotation FROM quotations LEFT JOIN authors USING (author_id)

WHERE quotations.quotation ORDER BY authors.family_name

Searching for quotations that contain a word or phrase

This illustrates an important difference between SQL and PHP. When it encounters OR, the PHP engine doesn’t bother to evaluate the second half of the condition if the first half is true. In a SQL query, however, both sides are evaluated. So, in the first case, the SQL query finds authors whose first name begins with “W” AND whose family name is anything. In the second case, it finds authors whose first name begins with “W” OR whose family name is anything. Creating searches with wildcards can be confusing, so it’s a good idea to display the SQL query onscreen while testing to understand why you get the results you do.

4.Add LIKE %var1%to the end of the WHEREclause, click the plus button alongside Variables, and define the runtime variable var1using the following settings:

Name:var1 Type:Text Default value:-1

Runtime value:$_GET['searchTerm']

The settings in the SQLand Variablesfields should now look like this:

5.Click OK to close the Recordset dialog box, save the page, and load it into a browser. The quotations contain a lot of seasonal references, so enter summeror winterin the Search forfield. You should see a list of quotations that contain the search term.

6.Searches with the %wildcard aren’t limited to single words. Try entering just xin the Search forfield. You should see a quotation from Winston Churchill that contains the word “except.”

7.You can also search for a phrase. Enter red, red rose, and click the Searchbutton.

You should see the following result:

Note that the phrase must be exact and must not be enclosed in quotes.

Check your code, if necessary, against find_quote_02.phpin examples/ch17.

17

This type of wildcard search works fine for even quite large databases. I use it on a data- base that contains more than 14,000 records, and the search results are normally displayed in one or two seconds. If you need to do a lot of text searches, you might consider FULL- TEXTindexing, which offers a more sophisticated range of text search options.

Một phần của tài liệu The Essential Guide to Dreamweaver CS4 with CSS, Ajax, and PHP phần 9 ppsx (Trang 20 - 24)

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

(94 trang)