我创建了以下触发器:
DELIMITER //
DROP TRIGGER IF EXISTS after_insert_order_replica
//
CREATE TRIGGER after_insert_order_replica AFTER INSERT ON order_replica
FOR EACH ROW BEGIN
INSERT INTO exm_order
(replica_id)
VALUES (NEW.id);
END;
//
DELIMITER ;
当我尝试从 PHP 执行它时,它显示了以下错误:
Error performing query 'DELIMITER // DROP TRIGGER IF EXISTS after_insert_order_replica; ': You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'DELIMITER // DROP TRIGGER IF EXISTS after_insert_order_replica' at line 1
Error performing query '// CREATE TRIGGER after_insert_order_replica AFTER INSERT ON res_order_replica FOR EACH ROW BEGIN INSERT INTO res_order ( replica_id ) VALUES ( NEW.id); ': You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near '// CREATE TRIGGER after_insert_order_replica AFTER INSERT ON res_order_replica ' at line 1
Error performing query 'END; ': You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'END' at line 1
Error performing query ' // DELIMITER ;': You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near '// DELIMITER' at line 1
我尝试使用不同的 DELIMITER 但没有成功。
这是我的 PHP 代码
public function Triggers(){
$restaurants = $this->em->getRepository('entities\company')->findBy(
array("companyName" => "testing_trigger")
);
set_time_limit(0);
include APPPATH . 'config/database.php';
$data = array();
$sql = "SHOW VARIABLES LIKE 'basedir'";
$result = $this->common->runSQL($sql);
$data['folderPath'] = rtrim(rtrim($result[0]['Value'], "/"), "\\");
$data['folderPath'] .= "/bin";
foreach ($restaurants as $rts) {
$data['dbHost'] = $rts->getServer()->getDbHost();
$data['dbUsername'] = $rts->getServer()->getDbUsername();
$data['dbPassword'] = $rts->getServer()->getDbPassword();
$data['dbDatabase'] = $rts->getDbDatabase();
$triggerAry = array(
"After Insert Order Replica" => "after_insert_order_replica.txt"
);
foreach ($triggerAry as $k=>$v) {
$trigger = FCPATH . "assets/$v";
$this->IMPORT_TABLES($data['dbHost'],$data['dbUsername'],$data['dbPassword'],$data['dbDatabase'], $trigger);
usleep(100000); //1 of 10th of a Second
}
}
}
function IMPORT_TABLES($host,$user,$pass,$dbname, $sql_file_OR_content){
set_time_limit(3000);
$SQL_CONTENT = (strlen($sql_file_OR_content) > 300 ? $sql_file_OR_content : file_get_contents($sql_file_OR_content) );
$allLines = explode("\n",$SQL_CONTENT);
$mysqli = new mysqli($host, $user, $pass, $dbname); if (mysqli_connect_errno()){echo "Failed to connect to MySQL: " . mysqli_connect_error();}
$templine = ''; // Temporary variable, used to store current query
foreach ($allLines as $line) { // Loop through each line
if (substr($line, 0, 2) != '--' && $line != '') {$templine .= $line; // (if it is not a comment..) Add this line to the current segment
if (substr(trim($line), -1, 1) == ';') { // If it has a semicolon at the end, it's the end of the query
if(!$mysqli->query($templine)){ print('Error performing query \'<strong>' . $templine . '\': ' . $mysqli->error . '<br /><br />'); } $templine = ''; // set variable to empty, to start picking up the lines after ";"
}
}
} return 'Importing finished. Now, Delete the import file.';
}
最佳答案
我认为DELIMITER
技术只是命令行工具mysql的一个技巧。
执行 DROP ...
作为来自 PHP 的查询。然后从 PHP 执行整个 CREATE ... END;
。请勿在其中使用 DELIMITER
或 //
。
关于php - 执行触发器时出现 SQL DELIMITER 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52888400/