That's not caused by CB Code Field. That's just how PHP works. use statements MUST be at the top of the PHP file being executed. You can't put them inline wherever you like. So your options are to include a PHP file and run your code from there or to supply the full path. Example as follows.
FROM: Factory::getDbo();
TO: \Joomla\CMS\Factory::getDbo();
You shouldn't be nor need to call Joomla API directly. The benefit of CB is it's very platform agnostic. Meaning if you use our API as much as possible you don't really have to worry about what version of Joomla you're using. You can access CBs global database object with the following.
Code:
global $_CB_database;
$query = "SELECT " . $_CB_database->NameQuote( 'parent_id' )
. " FROM " . $_CB_database->NameQuote( '#__affiliate_tracker_accounts' )
. " WHERE " . $_CB_database->NameQuote( 'user_id' ) . " = " . $user->getInt( 'id', 0 );
$_CB_database->setQuery( $query );
$comid = $_CB_database->loadResult();
$query = "SELECT cb." . $_CB_database->NameQuote( 'firstname' )
. ", cb." . $_CB_database->NameQuote( 'lastname' )
. ", cb." . $_CB_database->NameQuote( 'user_id' )
. " FROM " . $_CB_database->NameQuote( '#__comprofiler' ) . " AS cb"
. " INNER JOIN " . $_CB_database->NameQuote( '#__affiliate_tracker_accounts' ) . " AS a"
. " ON a." . $_CB_database->NameQuote( 'user_id' ) . " = cb." . $_CB_database->NameQuote( 'id' )
. " WHERE a." . $_CB_database->NameQuote( 'id' ) . " = " . (int) comid;
$_CB_database->setQuery( $query );
$comProfiler_user = $_CB_database->loadObject();
Note I've modified your query to be safely escaped. Please be careful directly accessing the database if you do not know how to safely escape as you risk creating a vulnerability.