Error establishing a database connection,

Maybe if you configured the wp-config.php file like the documentation says (see https://wordpress.org/support/article/editing-wp-config-php/ )

Sample wp-config.php file (database section), per the above link:

// ** MySQL settings - You can get this info from your web host ** //
/** The name of the database for WordPress */
define( 'DB_NAME', 'database_name_here' );
/** MySQL database username */
define( 'DB_USER', 'username_here' );
/** MySQL database password */
define( 'DB_PASSWORD', 'password_here' );
/** MySQL hostname */
define( 'DB_HOST', 'localhost' );

Your code is proper for making a ‘manual’ connection to the database, but it does not set the DEFINES that WP will use for connections.