php mysql search data with multiple keywords - php

I currently have my query to have LIKE('%keyterm%'). It works fine if my data has one keyboard typed. But what if I want to type in two keywords. Then it will return false.
Example:
keyword : rock climbing
data in mysql: Rock Day Free Climb
This phrase should come out because it has Rock and Climb but it seems like LIKE is not enough?
What else would I need to get that query?
Thanks!

You'll need to split on the whitespace and have a LIKE('%keyterm%') statement for each. See php.net/explode

Return results containing either keyword1 or keyword2:
SELECT * FROM table WHERE data LIKE '%keyword1%' OR data LIKE '%keyword2%';
Return results containing both keyword1 and keyword2:
SELECT * FROM table WHERE data LIKE '%keyword1%' AND data LIKE '%keyword2%';

Related

PHP SQL Search query matching

I have a search page in PHP which uses the following query to fetch content from the database.
SELECT * FROM linkBase WHERE (`title` LIKE '%".$s."%') OR (`descr` LIKE '%".$s."%')
Here is how my DB looks like :
id |title |descr
-----------------------------------------
1 |Hello World |This is a hello world description
2 |PiedPiper |The silicon valley company PiedPiper
3 |StackOverflow |Ask questions on programming
Now, for example, if I run this query with the value of $s = "silicon valley", Then it fetches the row 2. This is fine. But what if the user enters the search term as silicon PiedPiper. It returns no results with the above SQL query. How can I modify the query in such a way that it will return row 2 with the second search term too.
Question summery :
How can I do a search query using PHP and SQL in such a way that a result will be returned even if the the user enters two words which are not placed consequent to each other in the DB
If you need to support an arbitrary order of the words in the search I suggest using the REGEXP MySQL function with a simple "or" regular expression like
SELECT * FROM linkBase WHERE (`title` REGEXP 'silicon|PiedPiper') OR (`descr` REGEXP 'silicon|PiedPiper')
so you can replace the whitespace with PHP and replace them with a pipe symbol (|) and the order of the words don't matter anymore.
This will select all rows that contain at least one of the words, if you need to match all words in the list another regular expression might be necessary.
LIKE '%{".$s."}%' or to be old school INSTR(title, '{'.$s.'}') > 0

Text matching in sql query (LIKE)

My Database:
ID place
-----------------------------------------------
1 Bhowali
2 Bangalore
3 Tumkur
My Query:
SELECT *
FROM table
WHERE place LIKE '%bhovali'
Question is: many users search wrong keyword on that time the query result should match and output correct one.. in my above query bhovali is a wrong word the correct word is bhowali. is there any way to get correct result from the query??..
may be this can help SELECT * FROM table WHERE SOUNDEX(name) LIKE SOUNDEX('bhovali') ;
This is a really loaded question... short answer is no. SQL is made for giving you what you want, not what you might want.
You would have to develop some kind of intelligence engine to recognize that there are similar names and search for those as it occurs.
in mysql you can use full text search if the column has the full text index as
select * from table match(name) against ('bhovali');

Searching a field for the whole word, and nothing but the word

I have a PHP interface with a keyword search, working off a DB(MySQL) which has a Keywords field.
The way in which the keywords field is set up is as follows, it is a varchar with all the words formatted as shown below...
the, there, theyre, their, thermal etc...
if i want to just return the exact word 'the' from the search how would this be achieved?
I have tried using 'the%' and '%the' in the PHP and it fails to work by not returning all of the rows where the keyword appears in.
is there a better (more accurate) way to go about this?
Thanks
If you want to select the rows that have exactly the keyword the:
SELECT * FROM table WHERE keyword='the'
If you want to select the rows that have the keyword the anywhere in them:
SELECT * FROM table WHERE keyword LIKE '%the%'
If you want to select the rows that start with the keyword the:
SELECT * FROM table WHERE keyword LIKE 'the%'
If you want to select the rows that end with the keyword the:
SELECT * FROM table WHERE keyword LIKE '%the'
Try this
SELECT * FROM tablename
WHERE fieldname REGEXP '[[:<:]]test[[:>:]]'
[[:<:]] and [[:>:]] are markers for word boundaries.
MySQL Regular Expressions
if you also search for the commas, you can be sure you are getting the whole word.
where keywordField like '%, the, %'
or keywordField like '%, the'
or keywordField like 'the, %'
maybe I didn't understand the question properly... but If you want all the words where 'the' appears, a LIKE '%word%' should work.
If the DB of words is HUGE MySQL may fail to retrieve some of the words, that can be solved in 2 ways...
1- get a DB that support bigger sizes (not many ppl would chose this one tho). For example SQL Server has a 'CONTAINS' function that works better than LIKE '%word%'.
2- use a external search tool that uses inverted index search. I used Sphinx for a project and it works quite good. This is better if you rarely UPDATE the rows of the data you want to search from, which should be the case.
Sphinx for example would generate a file from your MySQL table and use this file to solve the search (it's very fast), this file should be re-indexed everytime you do a insert or update on the table, making it a much better solution if you rarely update or insert new rows.
It looks like you have a one to many relationship going on within a column. It might be better to create a separate table for keywords with a row for each keyword and a foreign key to whatever it is you're searching on.
Doing like '%???%' is generally a bad idea because the DB can't make use of an index so it will scan the whole table. Whether this matters will depend on the size of data you're working with but its worth considering up front. The single best way to help DB performance is in the initial table design. This can be tricky to change later.

mysql search database against a query

I'm looking for a way to compare database values against a given query using MySql
(in oppose of searching the query in the db)
I will elaborate:
I have a table that holds comma separated keywords and a result for each block of keywords
for example :
col 1 col 2
Mercedes,BMW,Subaru car
Marlboro,Winston cigarette
today im taking the user query (for example - Marlboroligt)
as you can see if i will search for the value 'Marlboroligt' in the db i will get no results.
for that matter i want to search 'Marlboro' from the db inside the query and return 'cigarette'
Hope my explanation is sufficient and that this is actually possible :-).
Normalization will help you the most. Otherwise, search in comma delimited fields is discussed here:
Select all where field contains string separated by comma
...and to search 'Marlboroligt' instead of 'Marlboro Light', you can try looking into the LEVENSHTEIN function, or maybe the Soundex encoding (which looks like too little bang for too large a buck, but then again, maybe...).
I see the following possible solutions:
setup a keyword-search engine like Sphinx and use it to search keywords in your db
normalize your db - col1 must contain the only keyword
use like patterns
select col2 from mytable where col1 like "%Marlboro%"
like slows down your application and can have substring-related issues.

MySQL - Filter result

I want to filter result of my SQL query. I want to select everything that has some specific text in some column.
Example:
SELECT * FROM categories WHERE (name
has 'abc' values in it's value ex.
MyabcCategory)
Also maybe it is not very good idea to do that in query, maybe it is better to get all and then filter array instead? But I don't know how to do that aether.
Use LIKE with % wildcard:
SELECT * FROM categories WHERE name LIKE '%abc%'
This will give you all the records that have abc somewhere in them.
You can learn more about it here :)
You want to use the LIKE operator, with % to match any character before and after your specific word :
select *
from categories
where name like '%abc%';
But note that doing so, MySQL will scan each line of the table, every time the query is executed... which might not be great if you have a lot of data.
If you're searching for some kind of text, you might either want to :
Use a FULLTEXT index, if you're working with MyISAM tables.
Or, use a solution that's separated from MySQL, with a specific indexing/search engine, such as Solr.
SELECT * FROM categories WHERE name LIKE '%abc%'

Categories