issue with variable scope in foreach condition
<p>let have a look in my script. below i am getting no values in while i am assigning the value to variable $specdata</p>

<pre><code>$result = mysqli_query($connection, "select * from productrates");
if (mysqli_num_rows($result) &gt; 0)
{
  echo "&lt;div class='box-body'&gt;";

  $header='';
  $rowdata='';

$header='&lt;tr&gt;';

$field= mysqli_fetch_fields($result);


foreach ($field as $val) {

    if($val-&gt;name !="specs") 
    {
    $header.= "&lt;th style='background:#E1E1E1'&gt;".$val-&gt;name."&lt;/th&gt;";

    }


}


$header.='&lt;/tr&gt;';

while ($row = mysqli_fetch_array($result))
{

    $field= mysqli_fetch_fields($result);
    $rowdata.="&lt;tr class='linehover'&gt;";
// in this section i want to show $specdata values in div id="specs"  

foreach ($field as $val) 
{
    $specdata =''; 

    if($val-&gt;name =="specs") 
    {

        $specdata = $row[1];
        echo $specdata;echo "&lt;hr&gt;";

    }
    if($val-&gt;name !="specs") 
    {
        if($val-&gt;name == "Product")
        {
            $rowdata.= "&lt;td class='nohover'&gt;".$row[$val-&gt;name]."&lt;div id='specs'&gt;".$specdata."&lt;/div&gt;&lt;/td&gt;";

        }
        else
        {
            $rowdata.= "&lt;td&gt;".$row[$val-&gt;name]."&lt;/td&gt;";
        }
    }
}
$rowdata.='&lt;/tr&gt;';


} 
}

mysqli_free_result($result);

echo '&lt;table class="table table-bordered"  id="pricelist"&gt;'.$header.$rowdata.'&lt;/table&gt;&lt;/div&gt;';

// but every i am getting null value in that div
</code></pre>