Custom module block holding DB calls I am working on a project where I first was only designing the theme. However, I now need to create 5 already made custom pages or blocks in a page, which will be available as soon as theme is enabled.

Each page should hold its own code for grabbing information from external database. Now I am having a huge dilemma on deciding how to create those pages. Since there are DB calls made on each of them, I was suggested to create a custom module, which would display a block with the printed content from database. I tried creating "pvpstats.module" and "pvpstats.info" as normal for custom modules, and displaying it in sidebar_first. After I enable the module, nothing happens. The block is just not visible. I used the code below.

function pvpstats_block_info() {
  $blocks['pvpblock'] = array(
    'info' =&gt; t('Example Block'),
    'status' =&gt; TRUE,
    'region' =&gt; 'sidebar_first',
    'weight' =&gt; 0,
    'visibility' =&gt; 0,
    'pages' =&gt; 'admin*
coder*
node/add*
node/*/edit
node/*/devel
',
  );

  return $blocks;
}

/**
  * Implements hook_block_view().
  */
function pvpstats_block_view($delta = '') {
  $block = array();

  switch ($delta) {
    case 'pvpblock':
      $block['subject'] = t('Block Name');
      $block['content'] = theblock_contents();
      break;
  }

  return $block;
}

function theblock_contents() {
  return '
    $dbserver="serveradress"; 
    $dblogin="username";       
    $dbpass="dbpassword";     
    $dbname="databaseserver";    

                mysql_connect($dbserver, $dblogin, $dbpass);
                mysql_select_db($dbname);
                mysql_query("SET NAMES 'utf8'");

                $vypis =  mysql_query("SELECT * FROM characters WHERE (accesslevel &lt; '1') AND (pvpkills &gt; '0') order by pvpkills desc LIMIT 20"); 
                echo '&lt;table&gt;&lt;tr&gt;&lt;td&gt;&lt;center&gt;Nick&lt;/center&gt;&lt;/td&gt;&lt;td&gt;&lt;/td&gt;&lt;td&gt;Kills&lt;/td&gt;&lt;/tr&gt;';
         while($row = mysql_fetch_array($vypis))
                    {
                echo '&lt;tr&gt;&lt;td&gt;&lt;font color="green"&gt;';
                echo $row["char_name"];
                echo '&lt;/font&gt;&lt;/td&gt;&lt;td&gt;&amp;nbsp;&amp;nbsp;&lt;/td&gt;&lt;td&gt;&lt;font color="blue"&gt;';
                echo $row["pvpkills"];
                echo '&lt;/font&gt;&lt;/td&gt;&lt;/tr&gt;';

                    }
    echo '&lt;/table&gt;';
    ?&gt; 


The main point is that I need those blocks later included in my theme so that they are visible immediately after enabling the theme, if that is somehow possible.
In theblock_contents you are trying to return php code. Don't. Do not echo or print in it. If anything, build a string. It should look about like that:

/**
* custom html block
* @return string
*/
function theblock_contents() {
  $dbserver="serveradress"; 
  $dblogin="username";       
  $dbpass="dbpassword";     
  $dbname="databaseserver";    

  $result = '';

  mysql_connect($dbserver, $dblogin, $dbpass);
                mysql_select_db($dbname);
                mysql_query("SET NAMES 'utf8'");

  $vypis =  mysql_query("SELECT * FROM characters WHERE (accesslevel &lt; '1') AND (pvpkills &gt; '0') order by pvpkills desc LIMIT 20"); 
  $result .= '&lt;table&gt;&lt;tr&gt;&lt;td&gt;&lt;center&gt;Nick&lt;/center&gt;&lt;/td&gt;&lt;td&gt;&lt;/td&gt;&lt;td&gt;Kills&lt;/td&gt;&lt;/tr&gt;';
  while($row = mysql_fetch_array($vypis))
  {
    $result .= '&lt;tr&gt;&lt;td&gt;&lt;font color="green"&gt;';
    $result .= $row["char_name"];
    $result .= '&lt;/font&gt;&lt;/td&gt;&lt;td&gt;&amp;nbsp;&amp;nbsp;&lt;/td&gt;&lt;td&gt;&lt;font color="blue"&gt;';
    $result .= $row["pvpkills"];
    $result .= '&lt;/font&gt;&lt;/td&gt;&lt;/tr&gt;';
  }
  $result .= '&lt;/table&gt;';
  return $result;
}


It's worth to mention that your code was plainly invalid PHP, no matter if it was supposed to run in Drupal or not. You can't simply return PHP code as string and hope it'll be executed. Only the part about not using echo in hooks is really Drupal dependent.

Important note: if you want your modules to work stable, please don't use PHP SQL functions directly. Use db_select to obtain data. Connecting to multiple databases is pretty well documented too.