1

我有一个这样的数组

array([0]=> 'email@email.com', [1]=> 'email2@email.com', [2]=> 'email3@email.com');

我还有一张桌子,上面有数百封电子邮件。

我希望能够删除除数组中的电子邮件之外的所有这些电子邮件。

我尝试了这段代码,但没有任何反应:

    $emails = join(', ', $emails);
    $sql = "DELETE FROM emails WHERE customer_id='".$id."' AND email NOT IN(".$emails.")";
    $query = $this->db->query($sql);

谁能告诉我哪里出错了?

谢谢

彼得

4

6 回答 6

4

您需要在 php 中使用implode函数将隐蔽数组导入字符串。

还需要用引号将电子邮件的字符串值括起来:

$sql = "DELETE FROM emails WHERE customer_id='".$id."' AND 
        email NOT IN('".implode("','",$emails)."')";
于 2012-09-06T13:34:45.233 回答
3

也许:

$emails = implode("', '", $emails);
$sql = "DELETE FROM emails WHERE customer_id='".$id."' AND email NOT IN('".$emails."')";
$query = $this->db->query($sql);
于 2012-09-06T13:35:08.727 回答
0

您缺少引号:

$emails = implode("', '", $emails);
$sql = "DELETE FROM emails WHERE customer_id='".$id."' AND email NOT IN('".$emails."')";
$query = $this->db->query($sql);
于 2012-09-06T13:35:56.777 回答
0

您的数组包含文字,但这些文字并不是每个都被您的 IN 列表的引号括起来。

于 2012-09-06T13:35:59.973 回答
0

您更有可能没有在查询中使用引号包装单个电子邮件。mysql_error 应该已经确定了吗?

$emails = join('", "', $emails);
$sql = 'DELETE FROM emails WHERE customer_id="'.$id.'" AND email NOT IN("'.$emails.'")';
$query = $this->db->query($sql);

尝试回显 $sql 并看看你得到了什么(如果它没有多大意义,请在此处发布)。

EDIT: How dare 2 people post the same answer as me while I'm typing my answer! >:|

于 2012-09-06T13:38:40.357 回答
0

please try this.

$emails = join(',', $emails);
To avoid comma in last ,
$emails=trim($emails,',');
$sql = "DELETE FROM emails WHERE customer_id='".$id."' AND email NOT IN(".$emails.")";
$res = $this->db->query($sql);
于 2012-09-06T13:44:09.810 回答