Custom Database Table and foreach

Assuming you have table that’s prefixed with the WordPress prefix (even if it’s not the default one), and the table is called table. Then the following code should select everything, and allow you to go through each row. In this example, it goes through each row and outputs the content of the field foobar.

global $wpdb;
$results = $wpdb->get_results("SELECT * FROM ".$wpdb->prefix."table");
foreach ( $results as $row ) 
{
    echo $row->foobar;
};

See the Codex page on $wdpb