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 mmocr的训练错误,结果全为0
  • ¥15 python的qt5界面
  • ¥15 无线电能传输系统MATLAB仿真问题
  • ¥50 如何用脚本实现输入法的热键设置
  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀