How do I fetch a data from an external database into my wordpress homepage

Thanks everyone, I have fixed the problem. The reason why there was an error was because the data was fetched as an object, not string. All I had to do is use WordPress Helpers instead. I used the $wpdb function to connect to the external database, then used the helper get_var() to fetch the text from database, then styled the output variable to give the marquee effect. Here is the complete code below.

<?php
//external_db_fetch.php
$mydb = new wpdb( 'username', 'password', 
'database_name', 'localhost' ); 

$stext = $mydb->get_var("SELECT col2 FROM 
table")
?>

Save the file above anywhere on your local host. Don’t put it in header.php for security reasons. Just include it in your header with the right path
include('path/to/external_db_fetch.php');

Then , encapsulate the output

echo $stext;

in a div class marquee to where you want the code to display(in your header).

Then edit (or create) the class marquee in your theme’s stylesheet to give the desired styling. Thanks.