php - Loop through a MySQL database until there is a result, then break -


if @ else part, there code comment contains question. how repeat it, maybe different kind of loop? unfortunately, not know how.

for ($id = 0; $id <= 16; $id++) {     $count1++;     $count2++;     $sql = "select jedynki id = $count1 && f = '0' ";       $result = $conn->query($sql);     if ($result->num_rows > 0) {         // output data of each row         while($row = $result->fetch_assoc()) {             $a = $row['a'];             $a=substr($a,0,55);             // something...             break;         }     } else {         /*  if no results:               1. go top of sql query               2. increment $count1               3. try again             repeat these steps (very important), until there result.  */     }     $sql = " select jedynki id = $count2 && f = '1' ";                       $result = $conn->query($sql);     if ($result->num_rows > 0) {         while($row = $result->fetch_assoc()) {              $b = $row['a'];              $b=substr($b,0,55);     ?>         // something...     <?php         }       }                } 

i haven't been able test should work far can tell. increments count1 long result null (the loop hasn't started yet) or number of rows fetched 0 , queries database new incremented value of count1.

for ($id = 0; $id <= 16; $id++) {     $result = null;      {         $count1++;         $sql = "select jedynki id = $count1 && f = '0' ";           $result = $conn->query($sql);     } while(($result == null) || ($result->num_rows == 0));      while($row = $result->fetch_assoc()) {         // something...         break;     } } 

Comments