我正在使用 mysql,我想在 Joomla 3.4.8 中创建一个触发器。
我有一个可以在 mySQLWorkbench 中运行的 SQL 代码:
如果我在 Joomla Controller 中运行它:
$db = JFactory::getDbo();
$sql = $db->getQuery(true);
$sql = 'LOCK TABLES id0ap_virtuemart_products WRITE;
DROP TRIGGER IF EXISTS update_vm_products ;
DELIMITER //
CREATE TRIGGER update_vm_products
AFTER UPDATE
ON id0ap_virtuemart_products FOR EACH ROW
BEGIN
INSERT INTO id0ap_virtuemart_notifications
VALUES ("", NEW.virtuemart_product_id, "product","update");
END //
DELIMITER ;
UNLOCK TABLES;';
$db->setQuery($sql);
$db->execute();
我得到:
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 'DROP TRIGGER IF EXISTS update_vm_products ; DELIMITER // ' at line 2 SQL=LOCK TABLES id0ap_virtuemart_products WRITE; DROP TRIGGER IF EXISTS update_vm_products ; DELIMITER // CREATE TRIGGER update_vm_products AFTER UPDATE ON id0ap_virtuemart_products FOR EACH ROW BEGIN INSERT INTO id0ap_virtuemart_notifications VALUES ("", NEW.virtuemart_product_id, "product","update"); END // DELIMITER ; UNLOCK TABLES;
我找不到问题所在。但当我试图解决这个问题时,我发现了一些奇怪的事情。如果我修改sql删除DELIMITERS和DROP部分:
$sql = 'CREATE TRIGGER update_vm_products
AFTER UPDATE
ON id0ap_virtuemart_products FOR EACH ROW
BEGIN
INSERT INTO id0ap_virtuemart_notifications
VALUES ("", NEW.virtuemart_product_id, "product","update");
END ;';
无论您只是尝试创建触发器,它都会起作用并创建触发器,只有在添加 DROP 部分时才会失败。任何想法?有没有面向对象的方法来做到这一点?
提前致谢!
最佳答案
我不是joomla专家,但似乎joomla使用任何mysql api来执行sql语句,每次调用只能执行1条sql语句。尝试单独执行每个sql语句。
关于mysql - 您的 SQL 语法有错误;在 Joomla 中创建触发器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36984177/