Stop PHP foreach loop after start

Asked

Viewed 25 times

1

How to interrupt a foreach PHP based on a condition coming from the database after starting the loop?

        $smtm = Connect::getInstance()->query("SELECT * FROM table WHERE num IS NULL");
        foreach ($smtm->fetchAll() as $value) {
            //aqui vai o comando a ser executado.
            if (Connect::getInstance()->query("SELECT * FROM table WHERE num IS NULL")) {
                //encerra o loop se não encontrar NULL
                break;
            }
        }

Since the BD can be changed at runtime, I would like to break if the seat query meets the condition during the loop.

  • 1

    Still outside the scope of your question! Break is enough to stop a repeat loop, the problem is in your condition for such, I suggest you review your code so you can understand yourself what you want.

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.