OK, I downloaded it from the server but it hasn't formatted correctly so I've separated the lines as below and the error is now showing on line 38.

Code
<?php

/* PHP CODE HERE */

/* BODY HERE */

$body = <<<EOF

<?php

  // Open and select database

  $link = mysql_connect('server','username','password');
  if (!$link)
    {
    die('Cannot connect to server: '.mysql_error());
    }
  $db = mysql_select_db('database',$link);
  if (!$db)
    {
     die('Cannot select database: '. mysql_error());
    }

  // Find out post id's

      $query = "SELECT * FROM ubbt_TOPICS ORDER BY TOPIC_VIEWS DESC";
      $result = mysql_query($query);
      if (!$result)
        {
        die('Cannot read from database: '.mysql_error());
        }

  // Build table

      for ($i=0; $i <$num_results; $i++)
        {
        $rowid = mysql_fetch_array($result);
        $postid=$rowid['POST_ID'];
        $queryposting = "SELECT * FROM ubbt_POSTS where TOPIC_SUBJECT=".$topicsubject."";
        $resultposting = mysql_query($queryposting);
        if (!$result)
          {
          die('Cannot read from database: '.mysql_error());
          }
      echo '<div style="float:right">'.$topicviews.'</div>';
      echo '<div style="float:right">'.$topicsubject.'</div>';
      }
?>

EOF;

?>