Loop until it returns with PHP

I am generating random code and I need to check that the code is not already in the database. I assume that this requires some type of loop. I have my request for all the settings, and I need it to run the code block again if mysql_num_rows == 0 .

+8
loops php
source share
4 answers
 $key = true; while($key){ // Do stuff if(mysql_num_rows($result) > 0) $key = false; } 
+14
source share

Use the do...while :

 do { // Your logic } while (condition); 
+24
source share

A simple update to James L. script - a loop script to check if the database has a login ID. If exists, will add +1 after logging in:

  $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"; 
+2
source share

Here is a completely different route:

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

All Articles