skip to Main Content

I recently upgraded my company’s project PHP and Laravel versions:

PHP: 7.3.2 => 8.1.18
Laravel: 7.30.6 => 9.52.5

We are using PHP and Laravel for our backend, our frontend is Vue2.

We’re using the PDO class to create a connection to BaanDB which is one of the DBs our company uses, it works with Informix driver and the class we use for that is PDO. It always worked before the upgrade to the new versions of Laravel/PHP. We use env() to get sensitive variables like passwords and hostname – also that how it worked for a long time for us.

Since the update, .env file became inaccessible through anywhere in the code, so lines like: $pdo = new PDO(env('BAAN_DB_DSN_PDO'), env('BAAN_DB_USERNAME_PDO'), env('BAAN_DB_PASSWORD_PDO'), $options); which are taking values of important data from the .env file raised an error which looks like: PDO::__construct(): Argument #1 ($dsn) must be a valid data source name which basically means the .env file couldn’t be read and recieved a null (tested in debugger).

I did found a solution for the problem which also regarded that the issue is happening after upgrading Laravel version.

The solution was to run the following 2 commands: php artisan config:cache, php artisan config:clear.

This fixed the issue, but raised another one. The issue happens again if you reset apache on the server, it also happens again after certain different changes to the system. Do I have to write the 2 commands again after every change caused to the server? this might interfere with the regular work we had, Is there some other solution which is more globally?

Edit: After trying the solution @Refilon suggested in the answer, The first error was gone and I received the following error in my network tab: SQLSTATE=HY000, SQLDriverConnect: -11041 [Informix][Informix ODBC Driver]Unspecified System Error = -11041. I’ll also mention we installed a new Informix driver on the server: ibm.csdk.4.50.FC1.LNX. It was tested by my manager and seemed to work. But perhaps the correct syntax was changed? Would appreciate any solution regarding that error.

Thanks in advance.

2

Answers


  1. Chosen as BEST ANSWER

    Solved!

    The issue was that we needed to export the environment variables to Apache httpd.

    There is a file in the linux server within the path: /etc/sysconfig/httpd.

    We had to add the following lines to the file: LANG=C LD_LIBRARY_PATH=/opt/IBM/informix/lib/:/opt/IBM/informix/lib/cli:/opt/IBM/informix/lib/esql INFORMIXDIR=/opt/IBM/informix ODBCINI=/etc/odbc.ini

    Somehow everything worked fine in earlier versions of PHP/Laravel without these lines. After adding these lines we restarted Apache and the PDO connection was established successfully.

    P.S.: @Refilon solved the first error of PDO::__construct(): Argument #1 ($dsn) must be a valid data source name. So thanks for that too!


  2. I’m not sure why you would like to do this as Laravel can handle this for you. But here’s my answer.

    Don’t use the env() function in your code. Store the env() variables in a php file in the config folder and then use it in your code like so: config(‘filename.variable_name’);

    Example:

    Create a new config file. For example: config/db.php

    <?php
    
    return [
        'db' => env('BAAN_DB_DSN_PDO'),
        'username' => env('BAAN_DB_USERNAME_PDO'),
        'password' => env('BAAN_DB_PASSWORD_PDO'),
    ];
    

    Then in your code:

    $pdo = new PDO(config('db.db'), config('db.username'), config('db.password'), $options);
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search