我创建了以下触发器:
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执行它时,显示了以下错误:
执行查询'DELIMITER // //如果存在触发器则触发错误after_insert_order_replica; ':您的SQL语法有误; 检查与您的MariaDB服务器版本相对应的手册 在'DELIMITER附近使用正确的语法//如果存在则触发DRUG after_insert_order_replica'在第1行
执行查询'//创建触发器after_insert_order_replica时出错 在res_order_replica上插入后,对每个行开始插入 res_order(copy_id)VALUES(NEW.id); ':您输入错误 您的SQL语法;检查与您的MariaDB相对应的手册 服务器版本,可在'// CREATE TRIGGER附近使用正确的语法 after_insert_order_replica在res_order_replica之后插入 1
执行查询'END时出错; ':您的SQL语法有误; 检查与您的MariaDB服务器版本相对应的手册 在第1行的“ END”附近使用正确的语法
执行查询'// DELIMITER;'时出错:您的错误 SQL语法;检查与您的MariaDB服务器相对应的手册 在第1行的'// DELIMITER'附近使用正确语法的版本
我尝试了不同的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.';
}
答案 0 :(得分:0)
我认为DELIMITER
技术只是命令行工具mysql的一个技巧。
将DROP ...
作为来自PHP的查询。然后从PHP完成整个CREATE ... END;
。请勿同时使用DELIMITER
或//
。
答案 1 :(得分:0)
此问题的解决方案是删除名为IMPORT_TABLES的函数并使用以下PHP代码:
$command = "C:/xampp/mysql/bin/mysql -h ".$data['dbHost']." -u ".$data['dbUsername']." -p".$data['dbPassword']." ".$data['dbDatabase']." < ".$trigger;
system($command);