简体   繁体   中英

Fetching SQL rows by conditions

i trying to fetch some rows by some conditions , but its not working as i want; please take look on my code and share your ideas.

            $query = mysqli_query($sqli,"SELECT * FROM  `table` WHERE 
                `name`      =       '$nameFA'   OR 
                `name`      =       '$nameLA'   OR
                `ft`        LIKE    '%$nameFA%' OR
                `ft`        LIKE    '%$nameLA%' AND
                `information`   LIKE    '%$info%'
            ");

as you see, the input of name and ft may have two different values. and rows should be fetch if information have something like $info variable.

Try this way :

$query = mysqli_query($sqli,"SELECT * FROM  `table` WHERE 
    (`name`      =       '$nameFA'   OR 
    `name`      =       '$nameLA'   OR
    `ft`        LIKE    '%$nameFA%' OR
    `ft`        LIKE    '%$nameLA%')
    AND `information`   LIKE    '%$info%'
");

Or this way, since i we don't know what you want to do we can't know how to place parenthesis:

$query = mysqli_query($sqli,"SELECT * FROM  `table` WHERE 
    `name`      =       '$nameFA'   OR 
    `name`      =       '$nameLA'   OR
    `ft`        LIKE    '%$nameFA%' OR
    (`ft`        LIKE    '%$nameLA%' AND
        `information`   LIKE    '%$info%'
    )
");

ALWAYS add parenthesis when you have multiple AND OR AND OR in a statement.

consider :

  • a = being a dog
  • b = barking
  • c = walking

a OR b AND C is true when : (you are a dog, OR your are barking) AND you are walking

a OR (b AND C) is true when : you are a dog, OR (barking AND walking)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM