触发器创建只是不工作,我尝试了一切我能想到的,例如,像这样:
$this->db->query("DELIMITER //\r\n
CREATE TRIGGER `delete_post` BEFORE DELETE ON `posts`\r\n
FOR EACH ROW BEGIN\r\n
DELETE FROM page_content WHERE page_content.post_id = OLD.post_id;\r\n
END\r\n
//\r\n
DELIMITER ;");
无论我做什么,我感觉它都不会创建触发器,因为Codeigniter只在一行中创建SQL语句。无论有没有换行符,都得到以下消息:
错误编号:1064
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 'DELIMITER // CREATE TRIGGER `delete_post` BEFORE DELETE ON `posts` FOR EAC' at line 1
DELIMITER // CREATE TRIGGER `delete_post` BEFORE DELETE ON `posts` FOR EACH ROW BEGIN DELETE FROM page_content WHERE page_content.post_id = OLD.post_id; END // DELIMITER ;
在PHPMyAdmin中,触发器create顺便工作,所以SQL是有效的
2条答案
按热度按时间iyr7buue1#
在通过PHP执行时,您应该从触发器中删除分隔符。mysql_或mysqli_函数应该能够在没有分隔符的情况下执行触发器。
下面是如何做到这一点。
zpf6vheq2#
这是解决问题的最简单方法。
只是不要忘记在每一行的右双引号前加一个空格。