SQL Server Access from PHP (PDO) Not Working Despite Driver Installed & Enabled

后端 未结 3 1934
滥情空心
滥情空心 2020-12-18 16:10

I am trying to access an SQL Server.

UPDATE: In response to Clive\'s comments below I have also tried reinstalling the native ODBC driver but no change.

I\'v

相关标签:
3条回答
  • 2020-12-18 16:30

    i have fond some config in php.ini but was noted;

    ;extension=odbc
    ;extension=pdo_mysql
    ;extension=pdo_oci
    ;extension=pdo_odbc
    

    remove “;” before, restart service, and it is worked!

    0 讨论(0)
  • 2020-12-18 16:44

    Although it is a fairly old post, I've decided that it would be better to place my answer here instead of creating an other question...

    after 3 weeks of research all over the Web and testing a lot of hypothesis around php installation, php code and drivers installation, I finally found the solution to this question.

    THE PROBLEM:

    I have an app that is installed on WIN SERVER 2012 SP2 and running under IIS. The development of this app is made on a WIN 7 SP1 and running under 32-bit XAMPP 3.2.2. All of these are running PHP 5.6.

    Everything is working fine on the server but I keep having this message when executed locally on XAMPP:

    This extension requires the Microsoft ODBC Driver 11 for SQL Server to communicate with SQL Server

    Even though I've installed this driver many times (with other php drivers found here, configure my php.ini to load php_sqlsrv_56_ts.dll and php_pdo_sqlsrv_56_ts.dll), it keeps returning me the same message...

    The PDO command I've used to connect to distant MSSQL Server was:

    $db = new PDO("sqlsrv:server=192.168.165.XXX;Database=testDB;", "userName", "passWord");

    I have tried other drivers and it worked fairly well with ODBC driver;

    $db = new PDO("odbc:Driver={SQL Server};Server=192.168.165.XXX;Database=testDB;", "userName", "passWord");

    but I've found that there was some limitations with the later driver that I didn't experience with sqlsrv on the PROD server so I decided to push a little bit more my research to find a way to implement sqlsrv on my local machine...

    THE INVESTIGATION:

    Before connecting with PHP, I've tried to create System DSN (Data Source Name), with the ODBC Source Administrator (ODBCSA) application and test connection from there before trying to connect with my application. Something pretty strange I have discovered with Win7, is that there are two different ODBCSA applications.

    The 32-bit version of the Odbcad32.exe file is located in the %systemdrive%\Windows\SysWoW64 folder.

    The 64-bit version of the Odbcad32.exe file is located in the %systemdrive%\Windows\System32 folder.

    The 32-bit version of the ODBC Administrator tool displays 32-bit system DSNs, 32-bit user DSNs, and 64-bit user DSNs.

    The 64-bit version of the ODBC Administrator tool displays 64-bit system DSNs, 32-bit user DSNs, and 64-bit user DSNs

    Just to make sure what ODBCA your are using, check in Task manager under odbcad32.exe, if you see *32 on its right, that means its the 32-bit version.

    At first, I executed the 64-bit ODBCSA, created DSN using the brand newly installed ODBC Driver 11, called ODBC_11_64 and test the connection using the ODBCSA. All worked fine. When I tested my code I received an other error message:

    The specified DSN contains an architecture mismatch between the Driver and Application

    Ok then, I tried to create an new DSN using the 32-bit ODBCSA. It was strange at first, when I've discovered that there was no version number aside some driver names and I received this error from the ODBCSA:

    The setup routines for the ODBC Driver 11 for SQL Server could not be found. Please reinstall the driver.

    and an other message:

    Component not found in the registry

    It was obvious that there was an installation problem and the later message point me in the right direction, the Registry...

    Starting regedit as admin, I've found that all 64-bit ODBC drivers can be found here: HKEY_LOCAL_MACHINE\SOFTWARE\ODBC\ODBCINST.INI

    All 32-bit ODBC drivers can be found here: HKEY_LOCAL_MACHINE\SOFTWARE\Wow6432Node\ODBC\ODBCINST.INI

    While trying to access the 64-bit key (\ODBC Driver 11 for SQL Server), I was able to display key values but when I tried to access the 32-bit key, I received this message:

    Error Opening Key, ODBC Driver 11 for SQL Server Driver cannot be opened. An error is preventing this key from being opened. Details: Access is denied.

    THE SOLUTION:

    By right clicking on the 32-bit faulty key I was able to access permission interface. I was then able to add my username (I could also add a group) and granted full Control on the registry key.

    I then came back to creating an new DSN using the 32-bit ODBCSA and ODBC Driver 11 for SQL Server, tested the connection with SUCCESS! :-)

    I when back to my application and found that I was able to access the distant MSSQL SERVER using sqlsrv, on my local machine.

    All along it was the driver installation package that was faulty! I should had install permissions to SYSTEM, users and administrators to the key has it did with the 64-bit registry key for this driver.

    It was a pretty long story but I thought it was worth it, since a lot of persons tried to solve this complex problem, going in many different directions without success. Its also my contribution to all of you, who failed to find the solution but helped me in my investigation ;-)

    0 讨论(0)
  • 2020-12-18 16:46

    In the end I solved this by installing the "Microsoft® SQL Server® 2012 SP1 Command Line Utilities" from here (SqlCmdLnUtils.msi):

    http://www.microsoft.com/en-in/download/details.aspx?id=35580

    No idea why this worked but there it is.

    0 讨论(0)
提交回复
热议问题