How can PHP code be structured to ensure that the correct username and password are used when connecting to a MySQL database?

To ensure that the correct username and password are used when connecting to a MySQL database in PHP, you can store the credentials in variables and pass them as parameters to the mysqli_connect function. This way, the connection will only be established if the correct username and password are provided.

<?php
$servername = "localhost";
$username = "your_username";
$password = "your_password";
$database = "your_database";

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

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