1

after trying any hours to add fulltext indexes into my table I found that Like %searchword% works too like that. I get from a first query the array $keywordse and after that I create with $keywordsonetoeight = implode(', ', $keywordse[0]); a comma seperated list with this output: string(25) "firstword, secondword, , , , , ,". I use mysql and the table runs with MyIsam.

$keywordse looks like this:

    array(1) { [0]=> array(8) { ["keyword1"]=> string(0) "" ["keyword2"]=> 
string(5) "ballo" ["keyword3"]=> string(5) "ballo" ["keyword4"]=> 
string(0) "" ["keyword5"]=> string(0) "" ["keyword6"]=> 
string(0) "" ["keyword7"]=> string(0) "" ["keyword8"]=> string(0) "" } }

my query:

"SELECT *
FROM posts 
WHERE title, text, area, contact LIKE %'$keywordsonetoeight'% AND (autorid != $userid)
ORDER BY id DESC"; 

The output is NULL

This works:

 "SELECT *
FROM posts 
WHERE title LIKE '%firstword%' AND (autorid != $userid)
ORDER BY id DESC"; 

2 Answers 2

5

Don't use implode() function directly on your array because several empty elements do exist in the array. First unset the empty array elements from your $keywordse[0] array, and then use REGEXP for your search.

Your code should be like this:

foreach($keywordse[0] as $key => $value){
    if(empty(trim($value))){
        unset($keywordse[0][$key]);
    }
}

$conditions = implode("|", $keywordse[0]);

$query = "SELECT *
FROM posts 
WHERE (title REGEXP '{$conditions}' 
OR text REGEXP '{$conditions}' 
OR contact REGEXP '{$conditions}') 
AND autorid <> {$userid} 
ORDER BY id DESC";

// Now execute this $query

Here's the reference:

Sign up to request clarification or add additional context in comments.

10 Comments

Here I get ´array(0) { }´ too. But Ive checked that the array values are the same how in an column
@Bodoppels Are you still getting NULL? If yes then please update your question with the contents of $keywordse array.
No I get array(0) { }. If I echo $conditions I get string(17) "|ballo|ballo|||||". The columns have an index and are fulltext is there a problem maybe?
@Bodoppels Found the bug. $conditions = implode("|", $keywordse); should be $conditions = implode("|", $keywordse[0]);. I've updated my answer.
@Bodoppels Glad I could help. Cheers! :)
|
1

if you want compare all values in your array you can user REGEXP:

"SELECT *
FROM posts 
WHERE title REGEXP '$first_word|$second_word|$third_word' 
OR text REGEXP '$first_word|$second_word|$third_word' )  AND (autorid != $userid)
ORDER BY id DESC";

3 Comments

That doesnt change something, I get ´NULL´.
Thank you, I need the second query but I get NULL, if I change one ´'%$keywordsonetoeight%'´ with a word that in an column in posts exists it works otherwise with your code dont. Anything must be wrong with the syntax in the query at the array
try the query after updating

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.