0

I am searching for hats but I am getting the result Whoziewhatsit Creative which is not my desired result.

I am using $subsql.=" and BusType like('%".$_REQUEST["what"]."%') "; code where $_REQUEST["what"]="hats"; and I am getting mysql error if I use a white space of the both side like $_REQUEST["what"]=" "."hats"." ";

Give me an expert solution please.

dersvenhesse
  • 6,276
  • 2
  • 32
  • 53
  • 1
    First of all, your code is highly insecure due to possible SQL-Injections. Second of all, please write the particular SQL error message and the Query which is executed in this case. This would help to see the actual problem. – Flixer Feb 05 '14 at 16:18
  • possible duplicate of [Search for "whole word match" in MySQL](http://stackoverflow.com/questions/656951/search-for-whole-word-match-in-mysql) – davidkonrad Feb 05 '14 at 16:22

3 Answers3

0

Having a LIKE '% hats %' clause is perfectly valid in SQL, although it would only match fields like 'Ken's hats and more' and not 'Ken's hats' or even 'hats' because those last two don't have the spaces.

Give us the entire query string and the SQL error message if something else isn't working.

Mordred
  • 3,734
  • 3
  • 36
  • 55
0

The correct query would be:

$subsql .= " and BusType LIKE '%". $what ."%'";

Be sure to check out mysqli_real_escape_string to improve your security :)

lsouza
  • 2,448
  • 4
  • 26
  • 39
0

Isouza is correct, and you can also use str_replace(' ','%',$what) in there if you have spaces in the $what value.

dsimer
  • 115
  • 6