Dynamically creating OR conditions by passing an array to a query in MySQL PHP

前端 未结 2 538
礼貌的吻别
礼貌的吻别 2021-01-23 16:23

I am trying to create OR condition dynamically using an array. Given an array, of course names $courses = array(\'Eng, \'Deu\', \'Bio\', \'Chemi\') I want to have a

2条回答
  •  迷失自我
    2021-01-23 16:53

    Instead of so many OR clauses, you can simply use IN(..):

    SELECT *
    FROM classe
    WHERE class = 'EFG' AND course IN ('Eng' ,'Deu', 'Bio')
    

    In the PHP code, you can use implode() function to convert the array into a comma separated string, and use it in the query string generation.

提交回复
热议问题