dpca4790 2013-11-25 20:04
浏览 89
已采纳

带有绑定参数的PDO sql query如果语句在更新中

I am attempting to create a 'trigger' not in the sql sense but I want to update the date_added field when the status field is set to 100

$sql='UPDATE table
SET status=:status,
    date_added=[PSEUDO CODE :status=100 ? now() : null;]
WHERE id=:id';

    $stmt=$conn->prepare($sql);
    $stmt->bindParam(':id', $id, PDO::PARAM_STR);
    $stmt->bindParam(':status', $status, PDO::PARAM_STR);
    $stmt->bindParam(':sign_id', $sign_id, PDO::PARAM_STR);
    $stmt->execute();
  1. Would it be better to attempt this in the sql query(unsure how to perform this) or on the php page (think I could stumble through that one) prior to issuing the query?
  2. Are there any performance gains one way or the other?

Thanks in advance for any help

  • 写回答

2条回答 默认 最新

  • duanlachu7344 2013-11-25 20:08
    关注
    date_added = :date
    
    $date = $status == 100 ? date('Y-m-d H:i:s') : null;
    $stmt->bindParam(":date", $date);
    

    You can do this comparison in MySQL as well using IF. I don't think that one is particularly faster than the other, but it makes more sense to me to use PHP for the comparison.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 matlab(相关搜索:紧聚焦)
  • ¥15 基于51单片机的厨房煤气泄露检测报警系统设计
  • ¥15 路易威登官网 里边的参数逆向
  • ¥15 Arduino无法同时连接多个hx711模块,如何解决?
  • ¥50 需求一个up主付费课程
  • ¥20 模型在y分布之外的数据上预测能力不好如何解决
  • ¥15 processing提取音乐节奏
  • ¥15 gg加速器加速游戏时,提示不是x86架构
  • ¥15 python按要求编写程序
  • ¥15 Python输入字符串转化为列表排序具体见图,严格按照输入