在触发器中使用分隔符的Mysql错误

时间:2015-08-07 10:37:07

标签: php mysql triggers phpmyadmin delimiter

这是我的mysql触发器声明

DELIMITER |
CREATE TRIGGER kk AFTER UPDATE
    ON location FOR EACH ROW
 BEGIN
        IF NEW.name not in(SELECT A.name FROM filter A  WHERE (NEW.name = A.name))THEN  

        IF NEW.location_name != OLD.location_name THEN
            INSERT INTO filter(old_location_name,new_location_name) 
            VALUES (OLD.location_name, NEW.location_name);

        ELSE

                UPDATE filter SET old_location_name = OLD.location_name , new_location_name = NEW.location_name WHERE name = OLD.name;  

        END IF; 

        ELSE

            UPDATE filter SET old_location_name = OLD.location_name , new_location_name = NEW.location_name WHERE name = OLD.name;


        END IF; 

  END;|
DELIMITER ;

当我在phpMyadmin中粘贴此代码时,这很好用。

在我的项目中

$sqlDrop = "DROP TRIGGER IF EXISTS `kk`";       
$resDrop = parent::_executeQuery($sqlDrop); 

echo $sql = "same above delimiter trigger query";
$rs = parent::_executeQuery($sql );

现在$sql语句在一行中回显

 DELIMITER | CREATE TRIGGER kk AFTER UPDATE ON location FOR EACH ROW BEGIN IF NEW.name not in(SELECT A.name FROM filter A  WHERE (NEW.name = A.name))THEN IF NEW.location_name != OLD.location_name THEN INSERT INTO filter(old_location_name,new_location_name,name) VALUES (OLD.location_name, NEW.location_name);    ELSE UPDATE filter SET old_location_name = OLD.location_name , new_location_name = NEW.location_name WHERE name = OLD.name; END IF; ELSE UPDATE filter SET old_location_name = OLD.location_name , new_location_name = NEW.location_name WHERE name = OLD.name; END IF; END;| DELIMITER ;

查询未执行。当我在我的phpMyadmin中粘贴echoed语句时,它显示我错误

enter image description here

那可能是什么问题。这是执行触发器查询的正确方法吗?

2 个答案:

答案 0 :(得分:3)

我不确定这对你是否真的有帮助。如何单独执行每个查询?

$startDelimit = 'DELIMITER |';
$resStart = parent::_executeQuery($startDelimit);

然后是你的创建触发器语句

$sql = "CREATE TRIGGER kk AFTER UPDATE ...... END IF; END;";
$rs = parent::_executeQuery($sql );

然后是最后一句话

$endDelimit = '| DELIMITER ;';
$resEnd = parent::_executeQuery($endDelimit );

答案 1 :(得分:2)

不确定任何与PHP相关的问题,但请尝试修改您的触发器代码,如下所示

DELIMITER |
CREATE TRIGGER kk AFTER UPDATE
    ON location FOR EACH ROW
 BEGIN
        IF NEW.name not in(SELECT A.name FROM filter A  WHERE NEW.name = 
A.name) AND NEW.location_name != OLD.location_name THEN      
            INSERT INTO filter(old_location_name,new_location_name) 
            VALUES (OLD.location_name, NEW.location_name);    
        ELSE    
                UPDATE filter SET old_location_name = OLD.location_name ,
 new_location_name = NEW.location_name WHERE name = OLD.name;      
        END IF;     
END;|
DELIMITER ;