I tried your codes this way, but it did not output anything:
<?php
$query="select from table1";
$result=mysql_query($query) or die(mysql_error());
while($row=mysql_fetch_array($result))
{
$teamid=$row["team_id"];
//team_id is a field of table 1;
$query2="select from table2 where id='$teamid'";
//I want to open another table in this loop; id is a field of table 2;
$result2=mysql_query($query2) or die(mysql_error());
while($row2 = mysql_fetch_array(mysql_query($query2))
{
echo $row2["anyfield"];
}
}
?>