MySQL SELECT combined with preg_match PHP

234 Views Asked by At

I have a SELECT that compare if there is an answer for a certain question for an account

SELECT * FROM questions WHERE questions.id NOT IN (SELECT answers.qid FROM answers where answers.account = '$account')

Then in the while, I have a preg_match that checks if the results are within a few other conditions Language - $langsession = a string that looks like "en,de,fr,etc" Category - $catsession = contains a one-word like "hotel"

The $lang contains a language for the question (ex. "en") and $qcat containing a category (ex. "hotel")

while($row = mysqli_fetch_array($result))
          {
        $lang = $row["lang"];
        $qcat = $row["category"];
        if (preg_match('/\b' . $lang . '\b/', $langsession) && $catsession==$qcat) { 
    //some code
}
        }

What I am trying to achieve is to put somehow if (preg_match... to the SELECT query.

1

There are 1 best solutions below

0
On

After rethinking my case I have decided to use FIND_IN_SET instead of regex.

Here is what did the trick for me

SELECT * FROM questions WHERE questions.id NOT IN
(SELECT answers.qid FROM answers where answers.account = '$account')
and FIND_IN_SET(questions.lang,'$langsession')
and category = '$catsession' order by section