带有MySQL查询的Foreach循环

I have an array.

$select_crm=mysql_query("select * from party_details where subcaseid='$under_row[partyid]'");
$select_array=array($select_crm);
if(mysql_num_rows($select_crm)>0)
 {
  foreach($select_array as $v)
  {
    $fetch_crm=mysql_fetch_array($v);
    echo $fetch_crm['party_name'];
    echo $fetch_crm['partyid'];``
  }
 }

But it can't works properly. $select_crm have two rows but it print only one.

As far as I'm concerned, you have to do this:

$select_crm=mysql_query("select * from party_details where subcaseid='{$under_row['partyid']}'");
if(mysql_num_rows($select_crm)>0)
 {
  while ($select_array = mysql_fetch_assoc($select_crm))
  {
    echo $select_array['party_name'];
    echo $select_array['partyid'];
  }
 }

Your code is iterating over an array which only 2 lines above you explicitly created to have exactly one element ;-)

You have misplaced the use of mysql_fetch_assoc. Check out the manual page and the sample code there for your solution. (and while you are there, notice the big red DEPRECATED notice; read on!).

$select_array is an array with just one element: the query result resource. Therefore the foreach loop will only ever run once, printing only the first item.

Your loop should look like every single tutorial out there:

while($fetch_crm = mysql_fetch_assoc($v)) { ... }

Note fetch_assoc, not fetch_array. It is pointless to call fetch_array if you don't intend to use the numeric indices.

$select_crm="select * from party_details where subcaseid='{$under_row[partyid]}'";
$crm = mysql_query($select_crm) or die(mysql_error());
$row_crm = mysql_fetch_assoc($crm);
$totalRows_crm = mysql_num_rows($crm);

if($totalRows_crm > 0) {
    do {
       /*foreach ($row_crm as $field) {
           echo $field;
       }*/

       echo $row_crm['party_name'];
       echo $row_crm['partyid'];
    } while ($row_crm = mysql_fetch_assoc($crm));
}

do not use foreach , you should use in this way ,

$select_crm= mysql_query("select * from party_details where subcaseid='$under_row[partyid]'");
while($result = $db->fetchByAssoc($select_crm))
{
   echo $result ['party_name'];
   echo $result ['partyid'];
}

try this

$result = mysql_query("your select query");
while($row = mysql_fetch_assoc($result)){
echo $row['columnNam1'];
echo $row['ColumnName2'];
}

Use this:

$select_crm=mysql_query("select * from party_details where subcaseid='$under_row[partyid]'");
while($row=mysql_fetch_assoc($select_crm)) {

echo $row['party_name'];

echo $row['party_id'];

}

try this:

 <?php 
 $select_crm = 
mysql_query("select * from party_details where subcaseid='$under_row[partyid]'");

 while($select_array = mysql_fetch_assoc($select_crm))
 {
    echo $select_array['party_name'];
    echo $select_array['partyid'];
  }
 ?>

hope it will help. happy coding!