Php is showing database passwords publicly [duplicate]

(just for understanding)

I have a very simple php code used to conncet to the database:

<?php
$servername = "localhost";
$username = "USER";
$password = "PASS";
$database = "DATABASE";

// Create connection
$conn = mysqli_connect($servername, $username, $password, $database);

// Check connection
if (!$conn) {
  die("Connection failed: " . mysqli_connect_error());
}
echo "Connected successfully";
?>

It is working great with no problems, but when I manually stop mysql service from XAMMP control panel, the browser is throwing an error including the credentials:

Fatal error: Uncaught mysqli_sql_exception: No connection could be made because the target machine actively refused it in C:xampphtdocsmirandafiles.php:36 Stack trace: #0 C:xampphtdocsmirandafiles.php(36): mysqli_connect(‘localhost’, ‘USER’, ‘PASS’, ‘DATABASE’) #1 {main} thrown in`

I am trying to understand why PHP is showing my credentials when mysql is stopped.

I have tried modifying if statement like this: (just removing . mysqli_connect_error())

if (!$conn) {
  die("Connection failed: ");
}
echo "Connected successfully";

hoping that php will not reveal my credentials anymore.
I need help to understand why this is happening and the mechanism of work.