How can I connect to a database? So I'm new to Drupal and am creating my first module. I can get the page to display html, but I'm not quite sure how to connect to the MySQL database. Here is my code:

&lt;?php
// $Id$

/**
 * @News
 * First module
 *
 * Just a simple news module
 */

function news_main() {
    db_set_active('default');
    $query = mysql_query("SELECT * FROM news");
    while ($news = mysql_fetch_object($query)) {
        $output .= '&lt;h1&gt;'.$news-&gt;title.'&lt;/h1&gt;&lt;p&gt;'.$news-&gt;body.'&lt;/p&gt;&lt;p&gt;'.$news-&gt;date.'&lt;/p&gt;';
    }
    return $output;
}

/**
 * Implements hook_menu().
 */
function news_menu() {
    $items = array();

    $items['news'] = array(
        'page callback' =&gt; 'news_main',
        'title' =&gt; 'News',
        'access arguments' =&gt; array('access content'),
        'type' =&gt; MENU_NORMAL_ITEM,
     );

    return $items;
}
?&gt;


I just get a blank page whenever I try to view it. I'm sure it's something simple, but I'm completely new to Drupal and it's API. I've looked through documentation for hours and couldn't figure it out.
So what's happening is you're querying a database that you're not connected to.

db_set_active('default');


...that makes the active database the Drupal default (the one it was installed on, usually).

$query = mysql_query("SELECT * FROM news");


...that doesn't actually do anything.  That is a MySQL Library function (i.e. not part of the Drupal API), so Drupal doesn't actually interact with it.

Now, depending on what you're trying to do here, it's not clear if you're trying to query a table that's part of the Drupal installation or if it's a secondary, external table in a database you're trying to connect to... however, based on the logic you're using I'm going to assume the table is in the Drupal database.

If news is a table in the default Drupal database, then what you want to use is db_query, and the API information for it is located here https://api.drupal.org/api/drupal/includes%21database%21database.inc/function/db_query/7.  You can read about interacting with the Drupal database through the Drupal API there.

Here's an example I pulled from the Migrate module:

$query = db_select('migrate_example_beer_topic', 'met')
         -&gt;fields('met', array('style', 'details', 'style_parent', 'region','hoppiness'))
         // This sort assures that parents are saved before children.
         -&gt;orderBy('style_parent', 'ASC');


Notice you don't actually have to specify the database you're connecting to, it just assumed 'default'.

In fact, for that matter, you should install the Migrate module and go through the examples that it has.  They come with the module, and will be accessible under sites/all/modules/migrate/migrate_example.