I have created the following trigger :
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 ;
When I tried to execute it from PHP it showed me the following errors :
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
I tried with different DELIMITER but no luck.
here is my PHP code
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.';
}
I think the DELIMITER
technique is just a trick for the commandline tool mysql.
Do the DROP ...
as a query from PHP. Then do the entire CREATE ... END;
from PHP. Don't use DELIMITER
or //
in either.
The solution for this problem is to delete the function called IMPORT_TABLES and use following PHP code:
$command = "C:/xampp/mysql/bin/mysql -h ".$data['dbHost']." -u ".$data['dbUsername']." -p".$data['dbPassword']." ".$data['dbDatabase']." < ".$trigger;
system($command);
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.