gpt4 book ai didi

php - 从 mysql 中删除批量 php 不起作用

转载 作者:行者123 更新时间:2023-11-29 10:36:36 25 4
gpt4 key购买 nike

我需要使用 pdo 对象从 mysql php 中删除批量

我有这样的数组

 $emails= array();
foreach ($items_emails as $datav) {

$emails[] = $datav[5];
}

然后我尝试使用 pdo 对象通过数组参数从 mysql 中删除

$sql = "DELETE FROM ".$data_table." WHERE email_address = :email_address";   
$stmt = $db_datas->prepare($sql);
$stmt->execute(array(':email_address' => $emails));

我收到此错误

数组到字符串的转换正是在这个语句中

 $stmt->execute(array(':email_address' => $emails)); 

最佳答案

"DELETE FROM x WHERE y = z"语法仅接受单个值进行比较,这就是传递数组失败的原因。您可以循环遍历值数组并单独删除条目,如下所示:

$sql = "DELETE FROM ".$data_table." WHERE email_address = :email_address";   
$stmt = $db_datas->prepare($sql);

foreach ($emails as $email) {
$stmt->execute(array(':email_address' => $email));
}

或者使用 WHERE x IN (list)将其变成单个查询:

$numberOfPlaceholders = count($emails);
$placeholdersArray = str_split(str_repeat('?', $numberOfPlaceholders));
$placeholders = implode(',', $placeholdersArray);
$sql = "DELETE FROM $data_table WHERE email_address IN ($placeholders)";
$stmt = $db_datas->prepare($sql);
$stmt->execute($emails);

您需要为列表中的每个元素创建一个单独的占位符,以便能够传递注释中指出的数组!

关于php - 从 mysql 中删除批量 php 不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46289413/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com