What im trying to do is order the information by farm_id and with each farm_id I want to be able to grab the last 10 results of the farms butterfat.

Unfortanly i get an error when it does the second loop. I've read on many forums where loops within loops are fine with php. but I keep getting this error.


$sqlk="SELECT * FROM `lanco_lab_quality`";
$resultsk=mysql_query($sqlk);
//Get the farm id's
while($datak=mysql_fetch_array($resultsk))
{
	$f1=$datak['farm_id'];
	echo "<br>";
	echo $f1;

$count=1;
$sqlj="SELECT * FROM `lanco_lab_quality` WHERE farm_id='$f1' ORDER BY `lanco_lab_quality`.`id` DESC ";
$resultsj=mysql_query(sqlj);
//Now get the last 10 results of butter fat for that farm id.
while($count<=10 && $dataj=mysql_fetch_array($resultsj))

	{
	echo $dataj['butterfat'];
	$count++;
	}

}	

Here is the result i get The number at the top is the value of the farm_id.
882
Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in /web/lanco/test.php on line 17

    I dont think it will solve this,
    but if a variable is an INTEGER NUMBER we do not use '$f1'
    but we use only $f1 in the SQL.
    If the variable have TEXT STRING we use '$string'

    WHERE farm_id=$f1 ORDER BY

    If you add LIMIT 10, then maybe you need no count loop.

        $sqlj="SELECT * FROM `lanco_lab_quality` WHERE farm_id='$f1'
     ORDER BY `lanco_lab_quality`.`id` DESC LIMIT 10";
        $resultsj=mysql_query(sqlj); 

      Updated PHP Script.

      $sqlk="SELECT * FROM `lanco_lab_quality`";
      $resultsk=mysql_query($sqlk);
      //Get the farm id's
      while($datak=mysql_fetch_array($resultsk))
      {
      	$f1=$datak['farm_id'];
      	echo "<br>";
      	echo $f1;
      
      $count=1;
      $sqlj="SELECT * FROM `lanco_lab_quality` WHERE farm_id=$f1 ORDER BY `lanco_lab_quality`.`id` DESC ";
      $resultsj=mysql_query(sqlj);
      //Now get the last 10 results of butter fat for that farm id.
      while($count<=10 && $dataj=mysql_fetch_array($resultsj))
      
      	{
      	echo $dataj['butterfat'];
      	$count++;
      	}
      
      }
        Write a Reply...