在更新sql表时递归发送电子邮件

时间:2013-04-04 10:55:39

标签: php mysql email send

我有以下查询:

UPDATE
    users AS t
    LEFT JOIN (
        SELECT
          user
        FROM
               facebook, users
        WHERE
            facebook.user = users.id
        GROUP BY
           users.id
    ) AS m ON
        m.user = t.id
SET
   t.coins =t.coins+200
WHERE
  m.user = t.id

我需要帮助来创建一个php脚本,它必须实际向用户发送一封电子邮件我更新硬币数量。电子邮件字段位于用户表中。

谢谢!

1 个答案:

答案 0 :(得分:1)

选择您需要的所有行:

SELECT
t.id, t.email
FROM
    users AS t
    LEFT JOIN (
        SELECT
          user
        FROM
               facebook, users
        WHERE
            facebook.user = users.id
        GROUP BY
           users.id
    ) AS m ON
        m.user = t.id
WHERE
  m.user = t.id

然后更新PHP循环中的每一行,您也可以发送邮件。

<强>更新

因此,假设$result是带结果数组的var。

foreach($result as $res){
    $sql = 'UPDATE users SET coins = coins+200 WHERE id = '.$res['id'];
    mail(/*WITH YOUR PARAMS*/); //email in $res['email'];
}