user4909046
user4909046

Reputation:

Concatenate two php variables in an sql query

So I have an sql query where I would like to query the database using the LIKE operator and the underscore wildcard where the first and last letter of a string are used to construct a pattern. I store the letters of a string in two php variables - $first(representing the first letter) and $last(representing the last letter) and from there they are used in the query. I have tried many different ways to get the query to work and so far I haven't had any success. Could you please look at my code and point out an error and/or suggest a correction. Any help will be appreciated.

$query2 = "SELECT name, description, subcategory, image, price FROM baby_products WHERE name LIKE '".$first."'+'_'+'".$last."'";

Upvotes: 1

Views: 2858

Answers (1)

Narendrasingh Sisodia
Narendrasingh Sisodia

Reputation: 21422

Try it out as

$query2 = "SELECT name, description, subcategory, image, price FROM 
baby_products WHERE name LIKE '%{$first}%' OR '%{$last}%'";

Upvotes: 2

Related Questions