What are the potential pitfalls when using MySQL in PHP code for database connections?

One potential pitfall when using MySQL in PHP code for database connections is leaving database credentials hardcoded in the code, which can lead to security vulnerabilities if the code is compromised. To address this issue, it is recommended to store the database credentials in a separate configuration file outside of the web root directory.

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

// connection.php
<?php
require_once 'config.php';

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

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