Sometimes you need the end user to type something simple into their search box but need the script you are writing to do something a little more complicated in the background. When searching an address, for example, you may need to accept a user search like:
153 Something Street
However, your database may well have the house/name number in separate columns in your database the table.
Searching across these columns is where concatenation comes to the rescue.
Let’s say there is a column called “houseNumber” and a column called “addressLine1”, for the sake of argument. When writing a query, you can join these two columns together, and add a space in between with this simple addition to the SQL code.
Carry on and select the columns that you need to, then just add your joined columns after the WHERE clause.
SELECT clientName, clientTelephone, houseNumber, addressLine1
WHERE CONCAT_WS(' ',houseNumber,addressLine1) LIKE ':searchQuery'
Then just insert the whole string (after sanitising if required), given via your user’s input, into the query.
$sql->execute(array(':searchQuery' => '%'.$_POST['userInput'].'%'));