Deepen your understanding of PHP and MySQL through this comprehensive tutorial that covers topics like wildcard searches and forms, and provides hands-on exercises for practical learning.
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.
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.
Simple Search
To search for a user by email, the syntax would be:
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.
Open search.php from the search folder.
-
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.
Switch back to your code editor.
-
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.
-
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.
-
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. -
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. -
Save the page and then in a browser go to:
- Mac: localhost:8888/phpclass/search/search.php
- Windows: localhost/phpclass/search/search.php
Enter a full or partial email and test it out. Excellent!
Switch back to your code editor.
Close any open files. That’s all folks!