while while循环使用mysql fetch循环

Im trying to run a while within a while. When the while loops runs for lets say the fifth time and its detects an item within the databases. I want it to print the item to the fifth loop.

But it reproduces itself up to the end of the loop (up to the 31th).

Does anybody know how to only print it to the loop when it loops itself the fifth time? And not to the end of the loop?

$num = 31
$i=0;
while($i<$num){
  $i++;
  mysql_num_rows(mysql_query("SELECT * FROM webCalendar WHERE day='$i'"))
  $sql = mysql_query("SELECT * FROM webCalendar WHERE day='$i'");
  while($result = mysql_fetch_object($sql)) {
    $info.= <<<EOD
    $result->descrip
    EOD;
  }
}
$num = 31
$i=0;
$is_found = false;
while($i<$num && $is_found == false){

  $i++;


  mysql_num_rows(mysql_query("SELECT * FROM webCalendar WHERE day='$i'"))
  $sql = mysql_query("SELECT * FROM webCalendar WHERE day='$i'");
  while($result = mysql_fetch_object($sql))
  {
    $info.= <<<EOD
    $result->descrip
    EOD;

    $is_found = true;

  }
}
  1. You can break from loops whenever you want, or else test for a flag in the loop condition (that you set once a result has been found);

  2. You don't need to search for results in this fashion, as MySQL can do it for you (which is much more efficient, especially if you have defined suitable indexes);

  3. You really shouldn't be using ext/mysql anymore—it has been deprecated as of PHP v5.5.0 and will be removed in the future.

Putting it all together, e.g. using PDO:

$dbh = new PDO("mysql:dbname=$dbname", $username, $password);
$qry = $dbh->query('
  SELECT descrip FROM webCalendar WHERE day = (
    SELECT MIN(day) FROM webCalendar WHERE day BETWEEN 1 AND 31
  )
');
while (($descrip = $qry->fetchColumn()) !== false) $info .= $descrip;