1

Can we delete set of triggers in mysql by passing an array to DROP TRIGGERS query from PHP?

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

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

Is this possible? Or any better way we can achieve this? Thanks in advance.

1 Answer 1

1

Something like this would do the job.

<?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);

Connect to the database, execute drop trigger for each trigger in array, close database connection

Sign up to request clarification or add additional context in comments.

2 Comments

Thank you for the response. But it query the DB for every trigger right? Can we drop set of triggers in a single database query. so that it will call DB only once.
Unfortunately no, MySQL doesn't support dropping multiple triggers with one query

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.