编程语言
首页 > 编程语言> > php – 如何在Codeigniter的迁移库中创建触发器

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
");

标签:php,mysql,codeigniter,codeigniter-2
来源: https://codeday.me/bug/20190825/1715365.html