How to Build an Example of a PHP Search Website

How to Build an Example of a PHP Search Website

Searching is the cornerstone of the Internet. Without it, we would be unable to find anything in that large body of information. So at some point your PHP website will probably need to provide search services. You can always embed Google search services into your website, but sometimes you need a search service specially suited to your site's content. How to make a specialized search function will vary according to your needs. However, building an example of a PHP search engine first will give you the experience you need to find new ways to expand on it.

Instructions

Set Up Your Database

    1

    Create a MySQL database. Go to your web hosting company's "control panel," the password-protected webpages where you can make changes to your website, and find the MySQL management section. Follow the instructions there to create a database called "example-search". Create a user for the new database named "example-search-user". Give the user whatever password you desire.

    2

    Find the section of your control panel where you can enter SQL commands for your new database. It will say something like "Enter SQL here" or "run this SQL on your database" followed by a text box. Such a screen will appear automatically in most web hosting control panels after you create a new database. Use that interface to run the following SQL command by copying and pasting this code into the text box:

    CREATE TABLE article (

    id SMALLINT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY,

    title VARCHAR(120) NOT NULL,

    article-text TEXT NOT NULL,

    FULLTEXT (title,article-text)

    )

    A new table will be created. It will contain articles with a title and a text body. MySQL will create a "fulltext" index for the table, meaning we will be able to quickly search the articles' whole bodies.

    3

    Populate your database. Use some articles from the web or make some up and put them in your "article" table so we have some content to search on. Your control panel should provide a web interface for creating entries in the database. (If you use other people's content, make sure you don't publish this search engine on the web, as that would be considered plagiarism.)

Create Your Search Function

    4

    Make an HTML file called "example-search.html". Place the file in your website's main folder--the folder you see when you first log in with FTP or go to your control panel's file upload center. Use this HTML for the page:

    Enter one search term:

    5

    Create a PHP file that will handle the search. Call it "example-search.php" and start it with the basic PHP opening tag:

    Save your file as "example-search.php" and place it in the same folder where you put the HTML file.

    6

    Set up your script's basic variables. First create a line of code setting the database user's password. In a business application, you would put this password in a separate file. For our tutorial, however, we will put it in the code following the pattern "$db_paswd='" followed by the password you chose for "example-search-user" and then "';". For example, if example-search-user's password is "abc123," then use the following line of code:

    $db_paswd='abc123';

    It is not recommended that you use "abc123" as your password.

    Connect to the MySQL database with this line of code:

    $dbh = mysql_connect('localhost', 'example-search-user', $db_paswd);

    Get the search term sent from the HTML form with this line of code:

    $keyword = $_REQUEST['keyword'];

    Now make the search keyword safe for the database with this line of code:

    $keyword = mysql_real_escape_string($keyword);

    7

    Run the article search. Set up your SQL query with this line of code:

    $query = "SELECT id, title, article-text FROM article WHERE title LIKE '%$keywords%' or article-text LIKE '%keywords%'";

    This query will look for all articles with the user's keyword in the title or body. Run the query on the database with this line of code:

    $sth = mysql_query($query, $dbh);

    8

    Display the results. Find out how many articles match the search keyword using this line of code:

    $result_count = mysql_num_rows($sth);

    If there are no matches, display a "no matches" result. Otherwise, display the results. Use this block of code to do that:

    if ($result_count == 0) //no results were found. Display a "no matches" message ?>

    No matches found.

    Title:

    Your script will output the title of each article that matches the search term.

    9

    Close your PHP script with the closing PHP tag like this:

    ?>

Blog Archive