Foreach loop to generate HTML table with title in first cell of each row in PHP
Is it critical that your product be the top row? It seems like you would benefit tremendously if your table was structured like this:

&lt;table&gt;
    &lt;thead&gt;
        &lt;tr&gt;
            &lt;th&gt;Product&lt;/th&gt;
            &lt;th&gt;Width&lt;/th&gt;
            &lt;th&gt;Material&lt;/th&gt;
        &lt;/tr&gt;
    &lt;/thead&gt;
    &lt;tbody&gt;
        &lt;tr&gt;
            &lt;th&gt;&lt;img /&gt;&lt;/th&gt;
            &lt;td&gt;50mm&lt;/td&gt;
            &lt;td&gt;Plastic&lt;/td&gt;
        &lt;/tr&gt;
    &lt;/tbody&gt;
&lt;/table&gt;


because that way, you can just use a single foreach loop:

foreach ($rows as $row) {
    $similar_com .= "&lt;tr&gt;";
    $similar_com .= "&lt;td&gt;&lt;img style='width: 100px; height: 100px;' src='$row-&gt;image_url' alt='$row-&gt;model_no' /&gt;&lt;br /&gt;$row-&gt;name&lt;/td&gt;";
    $similar_com .= "&lt;td&gt;$row-&gt;width mm&lt;/td&gt;";
    ...
}

You're doing a SELECT *. Avoid that; it's considered better to explicitly state the columns you're using.
I would rather rename your $rows variable to $products, but I notice that you already have a $product variable. I don't know what this variable is, but you should consider renaming some things... $similar_com is an especially useless name; I can't tell what it's supposed to mean.
I noticed you're using WordPress. The $wpdb-&gt;query(); $wpdb-&gt;last_result pattern is not as good as simply doing:

if ($rows = $wpdb-&gt;get_results($sql) {

If you have to keep your table structure the same, you could have separate variables for each row, then do it all in one foreach:

$widthRow = "&lt;tr&gt;&lt;th&gt;Width&lt;/th&gt;";
$materialRow = "&lt;tr&gt;&lt;th&gt;Material&lt;/th&gt;";
...
foreach ($rows as $row) {
    $widthRow .= "&lt;td&gt;$row-&gt;width mm&lt;/td&gt;";
    $materialRow .= "&lt;td&gt;$row-&gt;material&lt;/td&gt;";
    ...
}
$widthRow .= "&lt;/tr&gt;";
$materialRow .= "&lt;/tr&gt;";