How to use php array with sql IN operator?

后端 未结 13 1775
北恋
北恋 2020-11-29 06:23

I have and array with two values and I want to use it with sql IN operator in select query.

Here is the structure of my table

id comp_id
1   2
2   3
         


        
13条回答
  •  盖世英雄少女心
    2020-11-29 06:54

    If your array is of Integers :

    $searchStringVar = implode(",",$nameIntAryVar);
    $query="SELECT * from table NameTbl WHERE idCol='$idVar' AND comp_id IN ($searchStringVar)";
    

    If your array is of Strings :

    $searchStringVar = implode("','",$nameStringAryVar);
    $query="SELECT * from table NameTbl WHERE idCol='$idVar' AND comp_id IN ('$searchStringVar')";
    

提交回复
热议问题