2

I have the following code block which explodes a string upon spaces.

$run = explode(" ",strtolower($q));

eg. "This is the string"

Array $run look like:

(This,is,the,string)

The problems im having are:

I only want to explode the string if it has a white, something equal as using php function str_word_count($q)>1.

Unsure on how to create a single query which will work with multiple words in string and search table using any of them.

$query = "SELECT name FROM  `table1`  WHERE name LIKE '%". mysql_real_escape_string($run[0]) ."%' OR name LIKE '%". mysql_real_escape_string($run[1]) ."%'";

Trying to simplyfy the above query making it smaller and variable in size based on word count. Is this also a good aproach to exploding the string then preparing the sql?

I've tried using IN as well on SQL query with no good luck.

1 Answer 1

2

You can use MySQL's MATCH . . . AGAINST . . . to perform this kind of search:

WHERE MATCH (name) AGAINST ('this is the string')

Consult the documentation for more information.


Original solution, not helpful in this situation:

You can check out the MySQL specific function FIND_IN_SET:

 WHERE FIND_IN_SET(colname, 'This,is,the,string') > 0

The value in colname cannot contain a comma, however.

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.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.