数据库字段到MySQL查询

时间:2017-01-26 15:45:54

标签: php mysql

我正在寻找一种方法来将数据库中的字段用作查询。这就是我所拥有的:

列标题:banemail

使用vBulletin设置页面更新此字段的内容。它存储我想禁止注册的电子邮件地址。我需要一个查询,删除已注册的所有用户并使用禁止的电子邮件。我想从表中检索内容,然后使用它自己的查询。

到目前为止

查询:

$datastoreemails = $vbulletin->db->query_read("SELECT data FROM ".TABLE_PREFIX."datastore WHERE title = 'banemail'");

while($content = $vbulletin->db->fetch_array($datastoreemails))
  {
echo $content['data']."<br />";
  }  

此输出为:

  

.cc .co hotmail

如何将此输出转换为查询以删除数据库中具有上述电子邮件地址的任何人?

当我手动运行它时,这有效,但我有20-30个禁止的电子邮件地址,如果可能,我想在1个查询中完成所有操作。

DELETE FROM TABLE_PREFIX_user WHERE email LIKE '%.com%';

TIA

2 个答案:

答案 0 :(得分:0)

你可以直接使用select fro的结果

假设你有

  SELECT data FROM ".TABLE_PREFIX."datastore WHERE title = 'banemail'"

然后你可以

("DELETE 
  FROM " .TABLE_PREFIX ."user 
  WHERE email in (   SELECT data 
      FROM " .TABLE_PREFIX."datastore 
      WHERE title = 'banemail'" );

答案 1 :(得分:0)

我从未使用过vbulletin,因此您可能需要调整以下内容。我不知道用户表的例子是什么,所以我只输入了“用户”。



    $datastoreemails = $vbulletin->db->query_read("SELECT data FROM ".TABLE_PREFIX."datastore WHERE title = 'banemail'");

    //Create an array of banned emails.
    $bannedEmails = [];
    while($content = $vbulletin->db->fetch_array($datastoreemails))
    {
        //Explode your data by a space to return an array of items you are looking for
        $bannedEmails = array_merge($bannedEmails, explode(" ", $content['data']));
    }

    //check if there is anything in the banned Emails, if so build your "delete" query
    if(is_array($bannedEmails) && count($bannedEmails) > 0) :

        $deleteQuery = "DELETE FROM ".TABLE_PREFIX."users WHERE";

        $deleteWhere = "";
        //Loop through the array adding each email stub in to the query
        foreach($bannedEmails as $email):

            //If this is the second time through the loop add the AND clause
            if($deleteWhere != "") $deleteWhere .= "OR ";

            //Add in where email like %hotmail.com% to match anything that looks like it.
            $deleteWhere .= " email like '%" .$email . "%' ";
        endforeach;

        //Add the where on the end of the query
        $deleteQuery .= $deleteWhere;

        //Now all you need to do is execute the delete, I'm just going to print it out so you can check the
        //sql first!
        echo $deleteQuery;
    endif;

从您的代码中看起来有一个文本字段可以保存数据库中的禁止电子邮件地址。如果是这种情况,那么您可能希望使用“query_first”,如$ vbulletin-&gt; db-&gt; query_first($ query),因为它只会拉出一行,您不必循环结果。我已经编写了上面的代码,以防万行中有禁止的电子邮件,即使只有一行也应该有效。