微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

php – 如何在Codeigniter的迁移库中创建触发器

触发器创建只是不起作用,我尝试了所有我能想到的,例如,像这样:

$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中,触发器创建顺便进行,因此sql有效

解决方法:

您应该在通过PHP执行时从触发器中删除分隔符.
    MysqL_或MysqLi_函数应该能够在没有的情况下执行触发器
    分隔符.

这是怎么做的.

$this->db->query("
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
");

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐