I have installed latest Laravel 11.30 and it works good with MySQL. Now I need to connect to another server that is using SQL Server and read some data from a table. I have installed sqlsrv and pdo driver and odbc 18 but I am not sure should I config something else for using them or not? Both servers are windows, firewall on both are disabled. I am using xamp on windows to run php and mydql where Laravel istalled. Add SQL Server connection data to confug->database and add connection data to .ENV file write a little code to test it. Code follows:
Config->database
'sqlsrv_attendance' => [
'driver' => 'sqlsrv',
'host' => '192.168.10.13',
'database' => 'dbname',
'username' => 'user',
'password' => 'pass',
'charset' => 'utf8',
'prefix' => '',
],
env
#####SQL Server Added
DB_SQLSRV_HOST=192.168.10.13
DB_SQLSRV_PORT=1433
DB_SQLSRV_DATABASE=dbname
DB_SQLSRV_USERNAME=user
DB_SQLSRV_PASSWORD=pass
DB_OPTIONS="TrustServerCertificate=true"
DB_ODBC=0
#####################
Route
Route::middleware(['auth'])->group(function () {
Route::get('/test-query', function () {
try {
$data = DB::connection('sqlsrv_attendance')->select('SELECT TOP 1 * FROM DataFile');
dd($data);
} catch (\Exception $e) {
return $e->getMessage();
}
});
but I got this error.
SQLSTATE[08001]: [Microsoft][ODBC Driver 18 for SQL Server]SQL Server Network Interfaces: Connection string is not valid [87]. (Connection: sqlsrv_attendance, SQL: SELECT TOP 1 * FROM DataFile)
Googling the problem shows solving it with adding DB_OPTIONS="TrustServerCertificate=true"
in nodejs. What can I do? Should I config ODBC or DSN? I appreciate any help.
I have installed latest Laravel 11.30 and it works good with MySQL. Now I need to connect to another server that is using SQL Server and read some data from a table. I have installed sqlsrv and pdo driver and odbc 18 but I am not sure should I config something else for using them or not? Both servers are windows, firewall on both are disabled. I am using xamp on windows to run php and mydql where Laravel istalled. Add SQL Server connection data to confug->database and add connection data to .ENV file write a little code to test it. Code follows:
Config->database
'sqlsrv_attendance' => [
'driver' => 'sqlsrv',
'host' => '192.168.10.13',
'database' => 'dbname',
'username' => 'user',
'password' => 'pass',
'charset' => 'utf8',
'prefix' => '',
],
env
#####SQL Server Added
DB_SQLSRV_HOST=192.168.10.13
DB_SQLSRV_PORT=1433
DB_SQLSRV_DATABASE=dbname
DB_SQLSRV_USERNAME=user
DB_SQLSRV_PASSWORD=pass
DB_OPTIONS="TrustServerCertificate=true"
DB_ODBC=0
#####################
Route
Route::middleware(['auth'])->group(function () {
Route::get('/test-query', function () {
try {
$data = DB::connection('sqlsrv_attendance')->select('SELECT TOP 1 * FROM DataFile');
dd($data);
} catch (\Exception $e) {
return $e->getMessage();
}
});
but I got this error.
SQLSTATE[08001]: [Microsoft][ODBC Driver 18 for SQL Server]SQL Server Network Interfaces: Connection string is not valid [87]. (Connection: sqlsrv_attendance, SQL: SELECT TOP 1 * FROM DataFile)
Googling the problem shows solving it with adding DB_OPTIONS="TrustServerCertificate=true"
in nodejs. What can I do? Should I config ODBC or DSN? I appreciate any help.
1 Answer
Reset to default 1Your configuration from config/database.php
file is not affected by the contents on the .env
file because you are not overriding it.
Your code on config/database.php
should look as follows.
'sqlsrv_attendance' => [
'driver' => 'sqlsrv',
'host' => env('DB_SQLSRV_HOST', '127.0.0.1'),
'port' => env('DB_SQLSRV_PORT', 1433),
'database' => env('DB_SQLSRV_DATABASE', 'dbname'),
'username' => env('DB_SQLSRV_USERNAME', 'user'),
'password' => env('DB_SQLSRV_PASSWORD', 'pass'),
'charset' => 'utf8',
'prefix' => '',
'encrypt' => 'yes',
'trust_server_certificate' => 'true',
],