1

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."'";
4
  • If I correctly understand you're missing the CONCAT function in the MySQL Sting functions Commented Jun 5, 2015 at 10:29
  • 1
    Or you can simply write "SELECT name, description, subcategory, image, price FROM baby_products WHERE name LIKE '".$first.'_'.$last."'" Commented Jun 5, 2015 at 10:35
  • I tried both methods plus Uchicha's method. They all worked. Thank you Commented Jun 5, 2015 at 10:46
  • You are spoiled for choice! :D Commented Jun 5, 2015 at 10:59

1 Answer 1

2

Try it out as

$query2 = "SELECT name, description, subcategory, image, price FROM 
baby_products WHERE name LIKE '%{$first}%' OR '%{$last}%'";
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.