Medoo删除在数组中找到的ID

时间:2015-01-19 00:40:42

标签: php mysqli medoo

我有另一个使用Medoo进行数据库功能的编码器提供的脚本。我有一个脚本一次删除一个项目,但现在我需要根据复选框提交的列表删除多行。例如:

$delete = "123, 124, 125, 126";

global $db;
$sql = db::query("DELETE FROM users, [id IN '$delete']");

不幸的是,为Medoo提供的文档和示例很少,不包括IN示例。所以我在寻找建议。

3 个答案:

答案 0 :(得分:1)

真的很简单。只需提供带有ID的数组。

$database->delete("users", [
    "id" => [123, 124, 125, 126]
]);

答案 1 :(得分:0)

$delete = "123, 124, 125";        // original string

$delete = explode(',',$delete);   // convert to array

foreach($delete as $id) {         // walk wthrough array items

$database->delete("users", [      // run medoo query 
"AND" => [
    "id" => intval($id)          // if whitespaces, make an integer
    ]
]);
}

答案 2 :(得分:0)

请参阅medoo删除方法http://medoo.in/api/delete

$database->delete("account", array(
    "id" => array(123, 124, 125, 126)
));

$delete = "123, 124, 125";        

$ids_array= explode(',', $delete);  

foreach($ids_array as $id) {        
 $database->delete("users", array("id" => $id));
}

$ids_array= explode(',', '123, 124, 125'); 

function delete_by_id($value, $key){
  global $database;
  $database->delete("users", array("id" => $value));
}

array_walk($ids_array,"delete_by_id");

或......

$ids_array= explode(',', '123, 124, 125'); 
array_walk($ids_array, function($value, $key) use (&$database){
   $database->delete("users", array("id" => $value));
});

您可以查看上次执行的查询:

echo $database->last_query();