循环,直到PHP返回true

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.

$key = true;

    while($key){
        // Do stuff
        if(mysql_num_rows($result) > 0) $key = false;
    }

Use a do...while loop:

do {
  // Your logic
} while (condition);

Simple upgrade of James L. script - loop script for test if exist login ID in database. If exist, will add +1 after login:

    $key = true;
    $a = 1;
    $login_test_origin=$login_test;

    while($key){        
    $query_test="SELECT count(*) as 'all'  FROM user WHERE  login='$login_test'";
    $row_test=mysql_fetch_array(mysql_query($query_test));
    $error=$row_test[all];     

            if($error > 0) {
                $key = true;
                $login_test=$login_test_origin.$a;
                $a++;             
                           }

            else { 
                $key = false;
                $login=$login_test;
                 }
        }

echo"Used login ID: $login";

Here is a quite different route:

while(true){
    if(/* Your logic which you expect to be true */){
        break;
    }
}