1

I am using PHP with PDO to connect to my database.

I want to start using environment variables, so I used the following terminal command:

composer require vlucas/phpdotenv

My .env file now contains the following values:

DATABASE_HOSTNAME=db
DATABASE_NAME=myDb
DATABASE_USERNAME=user 
DATABASE_PASSWORD=test 

My test db connection file looks like this:

<?php
require_once __DIR__ . "/vendor/autoload.php";

$dotenv = Dotenv\Dotenv::createImmutable(__DIR__);
$dotenv->load();

$host = getenv("DATABASE_HOSTNAME");
$dbname = getenv("DATABASE_NAME");
$dbuser = getenv("DATABASE_USERNAME");
$dbpass = getenv("DATABASE_PASSWORD");

try{
    $dbc = new PDO("mysql:dbname=$dbname;host=$host;port=3306", $dbuser, $dbpass);
    $dbc->SetAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
  } catch(PDOException $e) {
    echo "Conneciton failed: " . $e->getMessage() . "<br/>";
  }  
?> 

I am getting the below error:

Conneciton failed: SQLSTATE[HY000] [2002] No such file or directory

I added this code to the test db connection file:

echo "X" . getenv('X');
echo "host is " . $host;

In the terminal, I enter: php test.php

And it echos out the following:

Xhost is %

Here is a screenshot:

enter image description here

What am I doing wrong?

5
  • 2
    Probably relevant in your case: github.com/vlucas/…. Tl;dr, don't use getenv and use $_ENV[...] instead.
    – Matthew
    Commented Jun 24 at 17:12
  • if you are using php with Apache as a webServer I'd use the builtin env variables available there instead of an external library in php. this way there are no files .env around Commented Jun 24 at 17:14
  • 1
    Not related to your problem, but getenv returns false if the variable doesn't exist, and when you concatenate that with the string X it gets converted to the empty string "". Based on your rep I'm guessing you knows this, but I just wanted to warn you that this can lead to a certain madness when debugging, especially if having empty ENV variables (for whatever reason) is something that you need to support.
    – Chris Haas
    Commented Jun 24 at 17:25
  • 1
    @Matthew - I made that change and now it works. Side note: this is the part of programming that irks me; the constant (and sometimes unnecessary) changes in the syntax, deprecations, etc. Although I understand it's part of the field of technology, I just find it extremely annoying. I should've been a chef. By the way - did you want to post an answer? I'll accept it. Commented Jun 24 at 17:38
  • 1
    It's good job security, half your time spent is fixing stuff that already worked.
    – Matthew
    Commented Jun 24 at 18:08

1 Answer 1

1

This is due to getenv and putenv not being populated by default with the Dotenv library.

Should use $_ENV instead to read the variables if using the createImmutable function, otherwise check out the docs at https://github.com/vlucas/phpdotenv?tab=readme-ov-file#putenv-and-getenv

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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