My computer has two php folders so I either can’t connect to SQL Server or install Laravel

  composer-php, laravel, php, sql-server

I spent two days setting up my PHP connection to SQL Server.

I downloaded the drivers, added them to the C:Program Filesphpext folder (as indicated by phpinfo()). Then I modified the php.ini file to add those extensions. Then I went to services and restarted "SQL Server EXPRESS". Nothing worked until I found that PHP is also installed in another folder on my PC: C:Program Filesiis expressPHPv8.0. So I added the drivers there too and changed the php.ini file and restarted the SQL Server EXPRESS service (is that what they mean with restarting your web server?). The phpinfo() page said:

Configuration File (php.ini) Path:  no value
Loaded Configuration File:          C:Program Filesiis expressPHPv8.0php.ini

and there was no section "sqlsserver". I gave up and the next day it magically worked. The phpinfo had a "sqlserver" section and the connection to my database no longer said "Call to undefined function sqlsrv_connect()".

Now, I had to download Laravel for my course, and install Composer. When doing so, it was unable to install when I had C:Program Filesiis expressPHP as my PATH and it asked me if I wanted to make C:Program Filesphp my PATH instead. So I clicked yes, and the installation was successful.

Laravel works, however now my database connections with SQL Server don’t work anymore! I get the same old "Call to undefined function sqlsrv_connect()" error, and my phpinfo() now says

Configuration File (php.ini) Path:
Loaded Configuration File:         C:Program Filesphpphp.ini
Scan for additional .ini files in: (none)
Additional .ini files parsed:      (none)

and there is no longer a "sqlsserver" section on the page.

I have tried everything in StackOverflow articles, such as removing the semicolon in the php.ini extension, including the full address in the extensions in the php.ini file, including the extensions of the other php folder into this php.ini file, restarting SQL Server service, etc. But nothing works. I also don’t know how to reset the old PHP folder back into the PATH variable.

My connection code is:

$con = sqlsrv_connect( 'localhost' ,
    [ 'Database' => 'sample_db' ,
        'UID' => 'sample_user' ,
        'PWD' => 'sample_password' ]
    );

//Print error message if the database doesn't connect

if ($con === false ){
    echo 'Failed to connect to db: ' . sqlsrv_errors()[ 0 ][ 'message' ];
    exit ();
}

How can I have these two php folder on my computer both work, the one for my PHP SQL Server database connection, and the other for my Laravel programme to run? Has anyone else had this issue and I so, what worked for you?

Source: Laravel

Leave a Reply