php - 执行触发器时出现 SQL DELIMITER 错误

标签 php mysql codeigniter orm mariadb

我创建了以下触发器:

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/

相关文章:

php - 非法字符串偏移 codeigniter

php - 确定哪个字段导致 Doctrine 重新查询数据库

php - 通过 WooCommerce 产品设置中的自定义复选框将类添加到 WooCommerce 页面

php - sql - 插入带条件的代码

javascript - 如何修复我的 [HY093] 错误。当我发送表格时就会发生这种情况

php - 我们可以从 codeigniter 中的单个模型方法执行两个查询吗?

php - 如何使用 PHP 将撇号从文本区域传递到 MySQL

php - 在 PHP 中解析自定义搜索语法?

MySQL group by desc -如何解释此sql : SELECT ts, avg_time FROM xx WHERE ip ="MAX"GROUP BY id DESC LIMIT 300?

php - Codeigniter 不显示页面