D.T.P
D.T.P

Reputation: 21

Connection error to SQL Server via PHP, SQL STATE IM004, Driver's SQLAllocHandle on SQL_HANDLE_ENV failed

I already have searched and googling about my question, but i still have not find the answer. My problem is when connecting to my SQL Server database via PHP PDO/ODBC Connection, i always getting error : "[Microsoft][ODBC Driver Manager] Driver's SQLAllocHandle on SQL_HANDLE_ENV failed, SQL state IM004 in SQLConnect". But my connection to Oracle or MySQL Database are not problem at all, only to SQL Server database.

Here my code to testing connection :

$dbh = null;
try
{
    $dbh = new PDO('oci:dbname='.TNS, DB_USERNAME, DB_PASSWORD, null);
    $dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
}
catch(PDOException $pe) {
    //var_dump($pe->getMessage());
}

if (!is_null($dbh)) {
    echo "Oracle Connection success";
}
else {
    echo "Oracle cannot connect";
}
echo "<br />";



$dbh = null;
try
{
    $dbh = new PDO('mysql:host=localhost;dbname=db_test', 'root', '', null);
    $dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
}
catch(PDOException $pe) {
    //var_dump($pe->getMessage());
}

if (!is_null($dbh)) {
    echo "MySQL Connection success";
}
else {
    echo "MySQL cannot connect";
}
echo "<br />";



$dbh = null;
try
{
    $dbh = new PDO('sqlsrv:server=XXXXX;Database=xxxxxxx', 'xxxx', 'xxxx', null);
    $dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
}
catch(PDOException $pe) {
    //var_dump($pe->getMessage());
}

if (!is_null($dbh)) {
    echo "MySQL Connection success";
}
else {
    echo "SQL Server cannot connect";
}

The results above is shown as :

I have tried to reinstall XAMPP and copying the new PHP's SQL Server library. But the error still same. As i mention above, i have been searching the solution, but still no luck. I truly hope, i can get a solution for my problem, or at least a clue for me to started with. Oh yeah, i almost forgot, this error occurs after Windows 10 update, before that, my connection to SQL Server is fine. So, i wonder whether this problem related to Windows update or not. I really have no clue.

Sorry for my English language, and thank you very much for your helps. I really apreciated for any answer

Upvotes: 2

Views: 3786

Answers (1)

rubendn
rubendn

Reputation: 205

I was having the same issue and these are the steps I took to make sure it was working correctly:

My Config (Win10, XAMPP, PHP 7.2.8)

1) Downloaded the Microsoft PHP Drivers for SQL Server 5.3 and put the following files in the C:\xampp\php\ext directory:

  • php_sqlsrv_72_ts_x86.dll
  • php_pdo_sqlsrv_72_ts_x86.dll

2) Added the following lines to my php.ini file in c:\xampp\php

  1. extension=php_pdo.dll
  2. extension=php_sqlsrv_72_ts_x86.dll
  3. extension=php_pdo_sqlsrv_72_ts_x86.dll

Note: I was originally missing the first line (extension=php_pdo.dll) and that gave me a "Driver's SQLAllocHandle on SQL_HANDLE_ENV failed" error. You do not need to put the dll in the ext directory because in PHP 7 it is built-in but you do need to put the line in the ini file.

3) Downloaded and installed Microsoft ODBC Driver for SQL Server 17.

Make sure you stop and restart the Apache server after making the changes.

Upvotes: 1

Related Questions