dou7466 2012-06-28 14:38 采纳率: 0%
浏览 59
已采纳

为什么我不能用PDO创建触发器?

I am trying to automate my trigger process so that I don't have to manually create triggers for every table I want to use. Unfortunately, I get an error back saying there is a syntax error.

Here is my code

$updateTrigger = "DROP TRIGGER IF EXISTS {$table}Update
    CREATE TRIGGER {$table}Update AFTER UPDATE ON $table
    FOR EACH ROW
    BEGIN
       DECLARE N DATETIME;
       SET N = now();
       INSERT INTO StagesHistory (Stage, StageID, Date, Action)
       VALUES ('$table', NEW.ID, N, ?);
    END";

$ut = $dbh->prepare($updateTrigger);
$ut->execute(array($update));
$error = $ut->errorInfo();

Evaluating $error returns this error message:

You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'CREATE TRIGGER TranslationUpdate AFTER UPDATE ON Translation FOR EACH R' at line 2

I can't figure out what error it's talking about. What's the problem, and how do I fix it?

  • 写回答

2条回答 默认 最新

  • dongxie45083 2012-06-28 14:43
    关注

    DROP TRIGGER ... and CREATE TRIGGER ... are two separate statements. You need to at least separate them with a ;, possibly execute them in separate queries.

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

报告相同问题?

悬赏问题

  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!
  • ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?
  • ¥15 求daily translation(DT)偏差订正方法的代码
  • ¥15 js调用html页面需要隐藏某个按钮
  • ¥15 ads仿真结果在圆图上是怎么读数的
  • ¥20 Cotex M3的调试和程序执行方式是什么样的?
  • ¥20 java项目连接sqlserver时报ssl相关错误
  • ¥15 一道python难题3
  • ¥15 牛顿斯科特系数表表示