PHP simple website search function

Source: Internet
Author: User
Search parameters in the search box of the database created in the document directory process part of the code function conclusion with the increasing number of website content and the increasing number of content on the network, the website search function has become an essential function of a website. Imagine that if a website with many resources does not have the search function, you need to find the desired

Search parameters in the search box of the database created in the document directory process part of the code function conclusion with the increasing number of website content and the increasing number of content on the network, the website search function has become an essential function of a website. Imagine that if a website with many resources does not have the search function, you need to find the desired

Document directory

  • Create a database
  • Search box
  • Search parameter Processing
  • Some code functions
  • Conclusion

With the increasing richness of website content and the increasing number of content on the Internet, website search has become an essential function for a website. Imagine how troublesome it would be to find the content that a user needs in the content of a website with many resources without the search function. Therefore, intra-site search is not only a simple query tool, but also a user experience related to website visitors. It can be said that searching is indispensable.

Create a database

Before formally writing code, we need to create a database. The following code can help us complete the process.

CREATE TABLE users (fname VARCHAR(30), lname VARCHAR(30), info BLOB);

We also need to insert some simple data:

INSERT INTO users VALUES ( "Jim", "Jones", "In his spare time Jim enjoys biking, eating pizza, and classical music" ), ( "Peggy", "Smith", "Peggy is a water sports enthusiast who also enjoys making soap and selling cheese" ),( "Maggie", "Martin", "Maggie loves to cook itallian food including spagetti and pizza" ),( "Tex", "Moncom", "Tex is the owner and operator of The Pizza Palace, a local hang out joint" )

Search box

Next, we will create a search box. The search box function mainly provides an entry for users to search, allowing users to enter keywords to search. This search box also serves to send text boxes or hidden parameters to the PHP file for data processing. Here we submit our own file: We can use the PHP_SELF () function to return our own path.

Search      
Search parameter Processing

After submitting the parameters, we naturally need to process them: query the information in the database through PHP and SQL statements.

 Search results

"; // No search keyword if ($ find =" ") {echo"

No keywords entered

"; Exit;} // otherwise we will connect to the database mysql_connect (" mysql.yourhost.com "," user_name "," password ") or die (mysql_error (); mysql_select_db (" database_name ") or die (mysql_error (); // process the keyword $ find = strtoupper ($ find); $ find = strip_tags ($ find); $ find = trim ($ find ); // now $ data = mysql_query ("SELECT * FROM users WHERE upper ($ field) LIKE '% $ find %'") is searched '"); // loop output result while ($ result = mysql_fetch_array ($ data) {echo $ result ['fname']; echo ""; echo $ result ['lname']; echo"
"; Echo $ result ['info']; echo"
"; Echo"
";}// The number of statistics results indicates $ anymatches = mysql_num_rows ($ data); if ($ anymatches = 0) {echo" sorry, no search results matching keywords found ...... ";}// The system prompts you to search for the keyword echo"Searched:". $ Find ;}?>
Some code functions

If ($ searching = "yes"): confirm whether the request has been submitted.
If ($ find = ""): determines whether the keyword is null.
$ Find = strtoupper ($ find): converts the keyword to uppercase (this search does not involve Chinese Characters in English only)
$ Find = strip_tags ($ find): filter HTML code
$ Find = trim ($ find): remove some special symbols
$ Data = mysql_query ("SELECT * FROM users WHERE upper ($ field) LIKE '% $ find %'"): query data
While ($ result = mysql_fetch_array ($ data): cyclic result
$ Anymatches = mysql_num_rows ($ data): number of search results

Conclusion

This is a very simple PHP + MYSQL application instance. You can take an introductory tutorial or try more search extensions: Search filtering, automatic search completion, etc ......

Statement: This article uses BY-NC-SA protocol for authorization | IT passers-
Reprinted please note

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.