Using wp config to connect to a DB from a plugin

I’m guessing this is a custom “plugin” where you are wanting to run a file externally that connects to the WordPress database, not really as a standard plugin which loads within WordPress environment, as in that case as @belinus says you can just use $wpdb class.

Anyway, since ABSPATH is defined IN wp-config.php, you can’t use ABSPATH to find and load wp-config.php – because it hasn’t loaded and thus isn’t defined yet. That’s the flawed logic… hence the errors you are getting.

If you know for certain that wp-config.php is in a directory above where the “plugin” file is, you can do something like this:

function find_require($file,$folder=null) {
    if ($folder === null) {$folder = dirname(__FILE__);}
    $path = $folder."https://wordpress.stackexchange.com/".$file;
    if (file_exists($path)) {require($path); return $folder;}
    else {
        $upfolder = find_require($file,dirname($folder));
        if ($upfolder != '') {return $upfolder;}
    }
}
$configpath = find_require('wp-config.php');

This will recursively search for wp-config.php above the plugin file directory and load it when it does. (Again this assumes the wp-config.php is located in a directory above the plugin file.)