cannot connect database

While it may possible to connect using mysqli->connect it is always better to make use of built in WordPress functionality (even for external code).

By this I mean that you should use the wpdb class. See https://codex.wordpress.org/Class_Reference/wpdb for more information.

First you need to link to the WordPress class:

echo '<h1>WP Connect DB</h1>';
echo '<p>Directory Check: '.$_SERVER['DOCUMENT_ROOT'] . '/wp-load.php</p>';
require_once( $_SERVER['DOCUMENT_ROOT'] . '/wp-load.php' );

If you get an error, check that the path to wp-load is correct and adjust if necessary.

Next you can define the user and database etc.

/** The name of the database for WordPress */
define('DB_NAME', 'mydatabase');
/** MySQL database username */
define('DB_USER', 'myusername');
/** MySQL database password */
define('DB_PASSWORD', 'mypassword');
/** MySQL hostname */
define('DB_HOST', 'localhost');

This info comes straight from the wp-config.php file. These do not need to be defined as constants, you can submit these directly into the right place in the database connection code below:

$wpdb = new wpdb( DB_USER, DB_PASSWORD, DB_NAME, DB_HOST);

Finally you can do something with the results for example:

$rows = $wpdb->get_results( "SELECT * FROM wp_posts" );
echo '<h3>Results</h3><pre>'.var_export($rows,true).'</pre>';

Putting it all together your code would look like this:

echo '<h1>WP Connect DB</h1>';
echo '<p>Directory: '.$_SERVER['DOCUMENT_ROOT'] . '/wpdev/wp-load.php</p>';
require_once( $_SERVER['DOCUMENT_ROOT'] . '/wpdev/wp-load.php' );
define('SHORTINIT', true );
/** The name of the database for WordPress */
define('DB_NAME', 'mydatabase');
/** MySQL database username */
define('DB_USER', 'myusername');
/** MySQL database password */
define('DB_PASSWORD', 'mypassword');
/** MySQL hostname */
define('DB_HOST', 'localhost');
$wpdb = new wpdb( DB_USER, DB_PASSWORD, DB_NAME, DB_HOST);
$rows = $wpdb->get_results( "SELECT * FROM wp_posts" );
echo '<h3>Results</h3><pre>'.var_export($rows,true).'</pre>';

The additional SHORTINIT definition is added to minimise the WordPress load but is not required.

I hope that this helps.