A MySQL solution
Depending on how you want the match to work, you could do something like the following:
$query = 'SELECT * FROM customers
WHERE lastName
LIKE CONCAT("%", :search, "%" )';
That would return rows where lastName had :search as a substring. CONCAT() is a variadic function which combines all of it's arguments into a single string.
CONCAT("%", "John", "%") = "%John%"
How it works
Why are the percents useful? MySQL's LIKE is a pattern matching keyword. LIKE has 2 special characters "%" and "_" which are used to signify "match any group of 0 or more characters" and "match any single character" respectively.
For example:
- "%john%" would match "johnson", "john", or "baker-johnson"
- "joh_" would match "john", but NOT "joh"
If you just care about matching rows whose last names start with a specific string, you would remove the first "%". This would have better performance and be able to make use of indexes if you make them.
Also, as noted in another answer, this concatenation could be performed in PHP if desired. This has the benefit of being more versatile if you wanted to let your users select the type of match to use (e.g. "Search whole string" vs. "Search from beginning of string")
Escape those wildcards!
If you do use a construct like this, be sure to escape LIKE's wildcard characters ("%" and "_") unless you want your users to be able to use the wildcard characters themselves. If you do let them use them you should add a LIMIT clause to your statement, or you run the risk of someone entering "%" and returning every row in your table.
If you want to escape things you can use a method like that provided in this answer:
https://stackoverflow.com/a/5020292
You could easily use PHP's str_replace() combined with the linked answer to perform your escapes. For example:
function search_customer($search) {
global $db;
$query = 'SELECT * FROM customers
WHERE lastName
LIKE CONCAT("%", :search, "%" ) ESCAPE "|"';
$escapedSearch = str_replace($search, ["%","_"], ["|%","|_"]);
$statement = $db->prepare($query);
$statement->bindValue(':search', $escapedSearch);
$statement->execute();
$results = $statement->fetchAll();
$statement->closeCursor();
return $results;
}