I added the custom.php file in the magento-2 root folder and I want to get the database details from env.php file

'db' => 
  array (
    'table_prefix' => '',
    'connection' => 
    array (
      'default' => 
      array (
        'host' => '',
        'dbname' => 'magento',
        'username' => 'root',
        'password' => '4gile-123',
        'active' => '1',

Can any please help to get database details.


If you check the magento vendor/magento/framework/App/DeploymentConfig/Reader.php

Check the function loadConfigFile

if ($fileDriver->isExists($path . '/' . $pathConfig)) {
    $configResult = include $path . '/' . $pathConfig;
    if (is_array($configResult)) {
        $result = array_replace_recursive($result, $configResult);

So I will keep it simple you can try this code in your custom.php file placed in magento root folder.

$envFilePath = __DIR__ . '/app/etc/env.php';
$configResult = include $envFilePath;
echo "<pre>";print_r($configResult);die;

Note: Make sure you have permission and you know what you are doing :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.