所以我希望能夠從數組中獲取值並在SQL查詢中使用它,並從這些行中獲取信息。我當前的代碼是像這樣:如何針對數組運行foreach並將其包含在SQL查詢中?
$config['exclude_devices'] = array('1','5');
$exclude = $config['exclude_devices'];
foreach ($exclude as $excluded) {
$deviceinformation = mysqli_query($db, "SELECT * FROM `devices` WHERE `device_id` NOT IN(" . $excluded . ")");
}
while ($row = mysqli_fetch_assoc($deviceinformation)) {
// do stuff here
// write to a variable for use in a table
}
然而,這給設備1,2,3,4的輸出。
如果我移動到foreach
包括while
,它產生2,3,4,5,1,2,3,4:
$exclude = $config['exclude_devices'];
foreach ($exclude as $excluded) {
$deviceinformation = mysqli_query($db, "SELECT * FROM `devices` WHERE `device_id` NOT IN(" . $excluded . ")");
while ($row = mysqli_fetch_assoc($deviceinformation)) {
// do stuff here
}
}
我怎麼能修改此代碼只能生產設備沒有在配置中列出?
你想要做什麼,這可能是有幫助的 - http://stackoverflow.com/questions/920353/can-i-bind-an-陣列到狀態 – WillardSolutions
選擇device_id不是「1」的記錄 - 選擇全部包括「5」。選擇device_id不是「5」的記錄 - 選擇全部包括「1」。爲什麼? –