Reputation: 31
Unfortunately, the ODBC driver for MSSQL does not always work with my PHP queries. I often get the error message:
SQLSTATE[IMSSP]: This extension requires the Microsoft ODBC driver for SQL Server to communicate with SQL Server. Go to the following URL to download the ODBC driver for SQL Server for x64: https://go.microsoft.com/fwlink/?LinkId=1637121
But when I refresh the page it may work again (or it may not work again and I have to refresh again).
When I search for the error message, I only find results that the driver should be installed. However, the driver is installed and is also displayed in the phpinfo().
phpinfo odbc:
phpinfo PDO:
It doesn't matter if the PDO connection is established via odbc or sqlsrv, the error message won't disappear permanently. It appears on a server with PHP 7 as well as on one with PHP 8.
Addition: The error occurs during the connection process. So I tried to put it inside a loop, so if the first connection try fails, a second or third might succeed. But even 100 tries fail. Either the first try succeed or it doesn't work at all.
while ($count < $maxTries) {
try {
$conn = null;
$conn = new PDO("sqlsrv:server=" . $host . " ; Database = " . $db . "", $user, $pw);
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$count = $maxTries;
echo "Connection SUCCESSFUL";
} catch (Exception $e) {
$count2 = 0;
while($count2 < $maxTries2) {
try {
$conn2 = null;
$conn2 = new PDO("sqlsrv:server=" . $host . " ; Database = " . $db . "", $user, $pw);
$conn2->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$count2 = $maxTries2;
echo "Connection2 SUCCESSFUL";
} catch (Exception $e1) {
$count2++;
echo "$count2 . Connecting to $host: <br>";
echo "$count2 Connection failed ... <br>";
#die(print_r($e->getMessage()));
}
}
$count++;
echo "$count . Connecting to $host: <br>";
time_nanosleep(0, 250);
// handle exception
if ($count == $maxTries) {
echo "$count Connection failed ... <br>";
die(print_r($e->getMessage()));
}
}
}
Afterwards, the $conn variable is still null and queries fail to execute.
Do you have any ideas on how I can solve this problem permanently?
Upvotes: 3
Views: 2684
Reputation: 50
I had the same problem even after installing the latest version of the ODBC driver (Version 18 at the time of writting), running PHP 7.0 on Windows
I solved the issue uninstalling that version and installing Version 17 of the ODBC driver
Upvotes: 2