Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Loop until returned true with PHP

Tags:

loops

php

I'm generating a random code, and I need to check to be sure that the code isn't already in the database. I'm assuming this requires some type of a loop. I have my query all setup and I need it to run a block of code again if mysql_num_rows == 0.

like image 493
DannyF247 Avatar asked Feb 27 '12 01:02

DannyF247


People also ask

Does Return break while loop PHP?

PHP 7 requires a return . A break; is not needed because the loop ends on return . A break; is usually used in a switch or loop whenever you have found your needed item.

Do-while VS while do PHP?

while is a pre-conditioned loop, and do.. while is post-conditioned loop. That means - in while a condition is checked before iteration, and in do.. while - after iteration.


1 Answers

$key = true;

    while($key){
        // Do stuff
        if(mysql_num_rows($result) > 0) $key = false;
    }
like image 154
James L. Avatar answered Oct 26 '22 22:10

James L.