One final addition to this that I didn’t think of mentioning earlier.
To use bbPress queries like these on a foreign (non-bbPress / non-WordPress) PHP page, all you need to do is include the bbPress core like this at the start:
<?php require('/local-path-to-bbpress/bb-load.php'); ?>
or to load WordPress core use this
<?php require('/local-path-to-wordpress/wp-config.php'); ?>
(change the ‘local-path’ bit to your local path)
and remember to use $wpdb on WordPress pages, vs $bbdb on bbPress pages.