Are there any potential risks or vulnerabilities associated with directly including database connection details in PHP files?

Including database connection details directly in PHP files can pose a security risk as it exposes sensitive information such as usernames and passwords. To mitigate this risk, it is recommended to store connection details in a separate configuration file outside of the web root directory and include it in your PHP files.

<?php
// config.php
define('DB_HOST', 'localhost');
define('DB_USER', 'username');
define('DB_PASS', 'password');
define('DB_NAME', 'database');

// connection.php
require_once('config.php');

$conn = new mysqli(DB_HOST, DB_USER, DB_PASS, DB_NAME);

if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
}