0

I got a database of two motorcycles:

  • yamaha R1 white 2000 and
  • yamaha R125 white 2005

When I send query:

SELECT * FROM bikes WHERE title LIKE '%$search%'

Then when I search for "yamaha R125" everything is ok but when you search "yamaha R1" then you will get two results yamaha R1 and yamaha R125.

Title anyone can specify as they want so title can be "yamaha R1 white 2005" but also "excellent condition yamaha R1".

Any idea how to sort it out?

tshepang
  • 12,111
  • 21
  • 91
  • 136
sieja
  • 17
  • 4

6 Answers6

1

MySQL will not be able to understand as long as you are using LIKE. There are just two solutions:-

1. Either segregate model and color from that single field and keep it in separate field 

2. SELECT * FROM bikes where title like 'yamaha R1%' AND title like '%2000';
Neeraj
  • 8,625
  • 18
  • 60
  • 89
  • that will work if people specific just name model and year of bike but when someone type "brand knew yamaha R1 2010 one owner" it wouldn't work – sieja Jan 10 '13 at 11:16
0

why you are using like clause, use title=$search

SELECT * FROM bikes WHERE title='$search'
Suhel Meman
  • 3,702
  • 1
  • 18
  • 26
  • but title looks like that "yahama R1 white 2000" when I type "yamaha R1" not full title just beginning still it's gonna work? – sieja Jan 10 '13 at 10:57
0

Try this SELECT * FROM bikes WHERE title = '$search'

If you want only 1 result

Like clause is used for wildcard search.If you use % sign it search like wild card

Check following link to know know more about String comparison

Hemantwagh07
  • 1,516
  • 1
  • 15
  • 27
0

the problem here is the $search value:

SELECT * FROM bikes WHERE title LIKE '%$search%'

with this you are trying to seach for titles that are equal to 'ANYTHING+$search+ANYTHING'

since you are searching for %yamaha R1% the results are:

yamaha R1 white 2000, yamaha R125 white 2005

Problem here is: if you use '%search' it won't work, cause you have the 'white 2000' at the end, why don't you set the color and the number in another field of the table and then search it like this:

SELECT * FROM bikes WHERE title LIKE '$search'

This should return all the info of yamaha R1, being one of the fields the color 'white' and another one the number '2000'

Naryl
  • 1,878
  • 1
  • 10
  • 12
  • Basically title anyone can type as they want so can be "brand knew yamaha R1 2010 one owner" then is more complicated – sieja Jan 10 '13 at 11:11
0

If these don't help

Match only entire words with LIKE?

mysql SELECT LIKE must match whole words only to the variable

then I am not sure it is possible in basic Mysql without changing schema, user input etc.

If you are on a dedicated server, or have a very good web host, I happen to know that the Sphinx Search engine http://sphinxsearch.com/ can work in the way you require.

Community
  • 1
  • 1
CodeMonkey
  • 3,271
  • 25
  • 50
0

This is what you want:

SELECT * FROM bikes WHERE  title REGEXP '[[:<:]]$search[[:>:]]' = 1;
Amorphous
  • 779
  • 7
  • 27
  • mysql_query("SELECT * FROM bikes WHERE title REGEXP '[[:<:]]'.$search.'[[:>:]]' = 1;"); gives "Warning: mysql_fetch_array() expects parameter 1 to be resource, boolean given in" – sieja Jan 10 '13 at 19:19