Search code examples
mysqlarrayssql-like

mysql - finding string from array


Database table:

id| p1 | p2 | notes
1 | 1  | a  | cat, mouse, dog
2 | 1  | a  | cat, horse, dog

I now need to run a query that selects the row where "notes" does not contain a string defined in the $exclusions array. I have tried the LIKE '%mouse%' operator, but that gave an error.

$exclusions = array ("mouse");
if($row['p1'] == 1 && $row['p2'] == "a" && $row['notes'] not like '%mouse%') {...}

Thank you.


Solution

  • Looks like you are doing the logic in a mix of PHP code and sql. To do it in php, you can do

    !strstr($row['notes'], 'mouse')
    

    That says "If there is no occurrence of "mouse" in $row['notes']"

    The ! will make it return true if there is no occurrence.