[英]How to delete record in bulk using php
$id=array(1,3,5);
$sql="delete * from tablename where id = ".$id;
Now I want to delete all record in mysql whose $id are 1,3 and 5 at once.. 现在我想要删除$ id为1,3和5的mysql中的所有记录。
DELETE FROM `tablename` WHERE id IN (1, 3, 5);
With PDO: 使用PDO:
$ids = array(1, 3, 5);
/* Create a string for the parameter placeholders filled to the number of params */
$place_holders = implode(',', array_fill(0, count($ids), '?'));
/*
This prepares the statement with enough unnamed placeholders for every value
in our $ids array. The values of the $ids array are then bound to the
placeholders in the prepared statement when the statement is executed.
*/
$stmt = $dbh->prepare("DELETE FROM tablename WHERE id IN (" . $place_holders . ")");
$stmt->execute($ids);
in mysql try below: 在mysql中尝试以下:
DELETE FROM tablename WHERE id IN (1, 3, 5)
https://dev.mysql.com/doc/refman/4.1/en/delete.html https://dev.mysql.com/doc/refman/4.1/en/delete.html
https://dev.mysql.com/doc/refman/5.0/en/comparison-operators.html#function_in https://dev.mysql.com/doc/refman/5.0/en/comparison-operators.html#function_in
deleting with an id: 使用id删除:
DELETE FROM `tablename` WHERE id = 1;
with IN clause: IN子句:
DELETE FROM `tablename` WHERE id IN (1, 3, 5);
delete all content from the given table 删除给定表中的所有内容
TRUNCATE table `tablename`;
deleting with given array of elements: 使用给定的元素数组删除:
foreach($id as $i) {
$sql="delete * from tablename where id = ".$i;
}
$id=array(1,3,5);
$ids = implode(",",$id);
$sql="delete * from tablename where id IN (".$ids.")";
use the above code. 使用上面的代码。
You first need to convert your array to the normal string, 首先需要将数组转换为普通字符串,
$ids = array(1, 3, 5);
$strngId = implode(",",$ids);
now with this variable you can write delete query. 现在使用此变量可以编写删除查询。
$sql = "Delete * from tablename where idfield IN (".$strngId.")";
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.