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条)

报告相同问题?