php – sqlsrv_connect works fine but laravel fails with : TCP Provider: No connection could be made because the target machine actively refused it-ThrowExceptions

Exception or error:

unfortuantely i have to make laravel talk to sql server .. i normally use mysql.. and have developed a swift hatred for sql server..

i’m running xampp on localhost for development. I can connect and query to local sql server fine with navicat and with sql server management studio.

sqlsrv_connect works fine in php, and i can run queries manually with no problem.
mssql_connect works fine in php, and i can run queries manually with no problem.

but laravel fails.

in env:

DB_CONNECTION=sqlsrv
DB_HOST=localhost
DB_DATABASE=handheld2
DB_USERNAME=handheld
DB_PASSWORD=thepasswordhere
DB_PORT=1433

and in laravel database config:

'sqlsrv' => [
            'driver' => 'sqlsrv',
            'url' => env('DATABASE_URL', 'localhost'),
            'host' => env('DB_HOST', 'localhost'),
            'port' => env('DB_PORT', '1433'),
            'database' => env('DB_DATABASE', 'handheld2'),
            'username' => env('DB_USERNAME', ''),
            'password' => env('DB_PASSWORD', ''),
            'charset' => 'utf8',
            'prefix' => '',
            'prefix_indexes' => true,
]

i’m not clear what ‘url’ is for? how is this different to db_host ?

i receive

Illuminate\Database\QueryException
SQLSTATE[08001]: [Microsoft][ODBC Driver 17 for SQL Server]TCP Provider: No connection could be made because the target machine actively refused it.

both php_sqlsrv_72_ts_x64.dll and php_pdo_sqlsrc_72_x64.dll are enabled in php

am i missing something simple and obvious?

edit: note that this code works

$connectionInfo = array(
    "Database"=>"handheld2",
    "UID" => "handheld",
    "PWD" => "thepasswordhere",
);
$conn = sqlsrv_connect("localhost", $connectionInfo);
if ($conn === false) {
    echo "Could not connect.\n";
    die(print_r(sqlsrv_errors(), true));
}
$stmt = sqlsrv_query($conn, 'SELECT top 1 * from users where users.id = 1');
if($stmt === false) {
    die( print_r( sqlsrv_errors(), true) );
}

while( $row = sqlsrv_fetch_array($stmt, SQLSRV_FETCH_ASSOC) ) {
    print_r($row);
}

and prints out record from db.. so why does it work here but not though laravel ???

How to solve:

debugging in laravels files in vendor.. turns out it was trying to connect to ‘andheld2’ instead of ‘handheld2’

changing

‘url’ => env(‘DATABASE_URL’, ‘handheld2’),

to

‘url’ => env(‘DATABASE_URL’, ‘/handheld2’),

in the database config makes it work.. it apparently ignores the first character.. for some reason? whatever.

Answer´╝Ü

If anyone else finds themselves here maybe this will help.

After hours of banging my head against the wall, the fix for me in the end was extremely simple. I was working with a fresh install of everything (including Sql Server) and I had to open Sql Server Configuration Manager (which itself is a pain in the butt in Windows 10…I ended up having to just search in the windows folder for SQLServerManager because I didn’t know the exact version number, which was 15).

Then > SQL Server Network Configuation > Protocols for MSSQLSERVER > TCP/IP was disabled. Enabled this, restart SQLSERVER in Windows Services and viola, laravel works.

Leave a Reply

Your email address will not be published. Required fields are marked *