You are missing to load files that will make $wpdb and other WordPress function available in the context. You can include some php files to emulate the WordPress context
include_once('../../../wp-config.php');
include_once('../../../wp-load.php');
include_once('../../../wp-includes/wp-db.php');
$data = $wpdb->get_results("SELECT * FROM " . $table_prefix . "posts");
var_dump($data);