Initializing connection to a MySQL database in PHP I wanted to get some feedback on this way of initializing a connection to a MySQL database.

File includes/db.php:

&lt;?php
$config_path = __DIR__ .  '/../config/database.ini';

$db_ini = parse_ini_file($config_path , true);
$db_info['db_host'] = $db_ini['db_data_source_name']['host'];
$db_info['db_user'] = $db_ini['db_general']['user'];
$db_info['db_pass'] = $db_ini['db_general']['password'];
$db_info['db_name'] = $db_ini['db_data_source_name']['dbname'];

foreach($db_info as $key =&gt; $value) {
    $upper_key = strtoupper($key);
    if (!defined($upper_key)) {
        define($upper_key, $value);
    }
}

$connection = mysqli_connect(DB_HOST, DB_USER, DB_PASS, DB_NAME);

?&gt;


Is there anything wrong with the file above being included in other php files as needed? 

Thank you!
Why the intermediate step of writing values to array? Why not:

define('DB_HOST', $db_ini['db_data_source_name']['host']);
define('DB_USER', $db_ini['db_general']['user']);
// etc.


Be careful with relative path with regards to including in other pieces of code.  If being included by code in another directory (particularly at a different level in directory structure), this may break.  Also, you won't be able to include this file more than once within any given script execution since you are defining constants.

Since you can only include this once per execution, do you really need to define constants at all?

$connection = mysqli_connect(
    $db_ini['db_data_source_name']['host'],
    $db_ini['db_general']['user'],
    $db_ini['db_general']['password'],
    $db_ini['db_data_source_name']['dbname']
);


I am just generally worried about your thinking of including this within other files "as needed".  That tends to make me think that you don't have a good strategy around managing your application dependencies (such as database connection).  Just sprinkling includes/requires throughout your code can really make your code fragile and hard to maintain.  If you really want to take you application coding to another level, I would suggest familiarizing yourself with dependency injection, both in a philosophical sense as well as from a practical standpoint looking at typical PHP libraries that do this (Pimple, PHP-DI, or pretty much any popular framework has some level of dependency injection strategy).

Hopefully your ini file is separate from your codebase (i.e. not under revision control) to where you are storing your DB passwords in your code.  If that is the case, consider taking the next step and have the ini file parsed in a different process such that this particular code is not responsible for reading configuration data from files that should perhaps should just be available in environmental variables.