Select from mysql table WHERE field='$array'?

后端 未结 2 1499
小鲜肉
小鲜肉 2020-11-29 09:38

If I have an array of say, some ID\'s of users. How could i do something like this:

$array = array(1,40,20,55,29,48);
$sql = \"SELECT * FROM `myTable` WHERE          


        
相关标签:
2条回答
  • 2020-11-29 10:01

    Use IN:

    $sql = "SELECT * FROM `myTable` WHERE `myField` IN (1,40,20,55,29,48)";
    

    you can use implode(",", $array) to get the list together from the array.

    0 讨论(0)
  • 2020-11-29 10:11

    You want to use IN:

    WHERE `myfield` IN (1,40,20,55,29,48)
    

    Use implode to construct the string:

    $sql = "SELECT * FROM `myTable` WHERE `myField` IN (" . implode(',', $array) . ")";
    
    0 讨论(0)
提交回复
热议问题