You can't output a graphic file in the middle of outputting an HTML file. Headers are already sent because you're already outputting HTML in the form of echo "<tr>" etc. The browser must receive a header with each page element. i.e., when you hit a web page, the browser first gets the HTML, then while rendering the page it must go fetch elements (such as IMG, LINK, EMBED)
It may be easier to visualize this as a separate PHP file, but you could do it inline as well:
<?php
// beginning of the script, capture output
ob_start();
// initialize
$db = mysql_connect('localhost','user','pass');
mysql_db_select('images_table');
// handle image requests
if (isset($_GET['file_id']))
{
$id = mysql_real_escape_string($_GET['file_id']);
$result = mysql_query("select blob_data from my_table where id = $id");
if ($blob = mysql_result($result,0,0))
{
header("Content-type: image/jpg"); // (or whatever it is)
echo $row['blob_data'];
exit;
}
}
// ... now do something else ...
ob_end_flush(); // output whatever you may have added before this part
echo "<table><tr><td>";
echo "<img src='".$_SERVER['PHP_SELF']."?file_id=123' />";
echo "</td></tr></table>";