SQL: Search

Deepen your understanding of PHP and MySQL through this comprehensive tutorial that covers topics like wildcard searches

Delve into the specifics of performing a basic wildcard search on a column in a database using PHP & MySQL. This tutorial will explore how to search for user information by email and the procedures to create a simple search form with search results display.

Key Insights

  • This tutorial focuses on conducting wildcard searches and creating a search form in PHP & MySQL.
  • The syntax for searching a user by email in the database is outlined, demonstrating the use of wildcard filters (%) to match any text before or after the entered search term.
  • It instructs on how to open 'search.php' and 'searchResults.php' from the 'search' folder, and how to navigate to these on Mac and Windows systems.
  • The page 'searchResults.php' has most of its content prewritten and includes a SELECT statement and results output to a table. The tutorial guides through adding the wildcard filters and formatting user input to be compatible with the parameterized query.
  • To modify the search term, the tutorial explains how to concatenate percent signs to the $_POST['email'] submitted from the search form. This step is crucial as PHP doesn't allow percent signs to be directly put in the SQL.
  • Finally, the tutorial explains how to bind this parameter to the query, save the page, test it out and close any open files.

This exercise is excerpted from Noble Desktop’s past app development training materials and is compatible with iOS updates through 2021. To learn current skills in web development, check out our coding bootcamps in NYC and live online.

SQL Search with LIKE

  • Wildcard Syntax

    WHERE email LIKE '%mySearchTerm%' — % matches any text before/after.

  • Single-Char Wildcard

    Underscore (_) matches exactly one character. WHERE name LIKE 'J_n' matches Jan, Jen, Jon.

  • Use Prepared Statements

    $stmt = $conn->prepare("... LIKE ?"); $stmt->bind_param('s', '%'.$term.'%');

  • Form Submits to Results Page

    Simple form posts to searchResults.php; that page runs the query and displays matches.

Topics Covered in This PHP & MySQL Tutorial:

Wildcard Searches, Searching with a Form

Exercise Overview

There are an enormous number of ways to search for information in a database—far more than can be covered in this book. We’ll show how to perform a basic wildcard search on a column.

To search for a user by email, the syntax would be:

SQL Bootcamp: Live & Hands-on, In NYC or Online, Learn From Experts, Free Retake, Small Class Sizes,  1-on-1 Bonus Training. Named a Top Bootcamp by Forbes, Fortune, & Time Out. Noble Desktop. Learn More.
SELECT * 
FROM users
WHERE email LIKE '%mySearchTerm%'

This will match any record in the database that has an email that contains text in the variable mySearchTerm. The % signs on either side of the search are the wildcard filters and mean to match any text before or after what the user enters.

  1. Open search.php from the search folder.

  2. In a browser go to:

    • Mac: localhost:8888/phpclass/search/search.php
    • Windows: localhost/phpclass/search/search.php

    It’s just a simple form with one input for an email. It submits to a page called searchResults.php which is the page that will perform the search and display the results.

  3. Switch back to your code editor.

  4. Open searchResults.php from the search folder.

    Most of the page has already been written. It contains a SELECT statement and outputs the results to a table. We’ll add the wildcard filters and format the user input so it is compatible with the parameterized query.

  5. Around line 5, modify the $SQL variable by adding the bold text as shown below:

    $SQL = 'SELECT id, firstName, lastName, email, publications, comments, subscribe 
    FROM users
    WHERE email LIKE ?
    ';

    This will filter the results to only display records that are LIKE the bound parameter.

Modifying the Search Term

Note in the syntax example at the beginning of the exercise that the search term is surrounded by percent signs. PHP won’t allow those percent signs to be put directly in the SQL—rather they need to be added to the search term itself.

  1. CONCATENATE percent signs to the $_POST['email'] that is submitted from the search form. At the top of the page above the $SQL variable, add the following bold code:

    $searchTerm = '%'. $_POST['email']. '%';

    This takes the email that is submitted from the form ($_POST['email']) and adds percent signs on either side of it.

  2. Finally we need to bind this parameter to the query. Around line 14 find the //bind params here comment and replace it with the bold code:

    $stmt->bind_param('s', $searchTerm);

    This binds the $searchTerm variable and tells PHP to expect a string.

  3. Save the page and then in a browser go to:

    • Mac: localhost:8888/phpclass/search/search.php
    • Windows: localhost/phpclass/search/search.php
  4. Enter a full or partial email and test it out. Excellent!

  5. Switch back to your code editor.

  6. Close any open files. That’s all folks!

Learn more in these courses

How to Learn Full-Stack Web Development

Master Full-stack Web Development with Hands-on Training. Build Fully Functional Websites and Applications Using HTML, CSS, JavaScript, Python, and Web Developer Tools.

Yelp Facebook LinkedIn YouTube Twitter Instagram