pdo update语句 - 在同一语句中保存和更新值?

时间:2013-12-26 01:59:17

标签: php mysql pdo prepared-statement

我将用户的最后和当前IP地址/日期时间存储在数据库中。成功登录用户后,我正在运行这两个准备好的语句。

出于好奇,有没有办法在一个声明中执行此操作?

由于订单的原因,是否允许lastIP = currIP, currIP = ?, dtLastLogin = dtCurrLogin, dtCurrLogin = UTC_TIMESTAMP()这样的内容?

// update user's last ip and last login date in db
$stmt = $db->prepare("UPDATE accounts SET lastIP = currIP, dtLastLogin = dtCurrLogin WHERE account_id = ?");
$stmt->execute(array($account_id));

// update user's current ip and current login date in db
$stmt = $db->prepare("UPDATE accounts SET currIP = ?, dtCurrLogin = UTC_TIMESTAMP() WHERE account_id = ?");
$stmt->execute(array($_SERVER['REMOTE_ADDR'], $account_id));

1 个答案:

答案 0 :(得分:1)

是的,使用mysql,你可以在一个sql中执行此操作。

$stmt = $db->prepare("UPDATE accounts SET lastIP = currIP, currIP = ?, dtLastLogin = dtCurrLogin , dtCurrLogin = UTC_TIMESTAMP() WHERE account_id = ?");
$stmt->execute(array($_SERVER['REMOTE_ADDR'], $account_id));

但请注意 订单很重要