Level 28
From your code, it looks like you are trying to connect to SQL server from your local machine. Can you confirm if the code and the SQL server reside in the same machine?
Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.
Error message:
SQLSTATE[08001]: [Microsoft][ODBC Driver 11 for SQL Server]TCP Provider: No connection could be made because the target machine actively refused it.
(SQL: select * from [TblEmployee])
when i try to connect to sql server using laravel i got above error, but i successfully connect using core php
<?php
$servername = "localhost";
$username = "username";
$password = "password";
try {
$conn = new PDO("sqlsrv:Server=$servername;Database=bcsdb_Staff", $username, $password);
// set the PDO error mode to exception
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
echo "<code>";
echo "Connected successfully<br>";
// fetch query
$sql = "SELECT * FROM TblEmployee";
// get result
$result = $conn->query($sql);
while ($row = $result->fetchObject()) {
echo "{$row->EMPNAME}";
}
$conn = null;
echo "</code>";
} catch (PDOException $e) {
echo "Connection failed: " . $e->getMessage();
}
my .env config
DB_CONNECTION=sqlsrv
DB_HOST=localhost
DB_PORT=1433
DB_DATABASE=bcsdb_Staff
DB_USERNAME=username
DB_PASSWORD=password
Please or to participate in this conversation.