query动态删除mysql触发器

ruoxqz4g  于 2021-06-25  发布在  Mysql
关注(0)|答案(1)|浏览(268)

我们可以通过从php传递一个数组来删除mysql中的触发器集吗?

$triggersToDelete = ['trigger1', 'trigger2', 'trigger6'];

$sql = 'DROP TRIGGER IF EXISTS $triggersToDelete';

这可能吗?或者有什么更好的方法可以达到这个目的?提前谢谢。

3yhwsihp

3yhwsihp1#

像这样的东西就行了。

<?php

$host = "localhost";
$user = "root";
$pass = "";
$database ="dbname";

$conn = mysqli_connect($host, $user, $pass, $database);

if (!$conn) {
    die("Failed to connect to the database");
}

$triggersToDelete = ["trigger1", "trigger2", "trigger6"];

foreach ($triggersToDelete as $trigger) {
    if (!mysqli_query($conn, "DROP TRIGGER IF EXISTS " . htmlspecialchars($trigger, ENT_QUOTES))) {
        echo "Failed to delete trigger: " . $trigger . "<br>";
    }
}

mysqli_close($conn);

连接到数据库,对数组中的每个触发器执行drop trigger,关闭数据库连接

相关问题