11

I trying to connect to an SQL Server in PHP. With XAMPP on my local machine, everything works well. But now I going to bring my application on the production server.

On this server there is installed the Microsoft IIS 6.1 and running the PHP version 7.0.7. I also installed the ODBC Driver from here. Next I decomment the following line in my php.ini file:

extension=php_sqlsrv_7_nts.dll
extension=php_pdo_sqlsrv_7_nts.dll

I got the files from the official microsoft site.

What's my problem?

Unfortunately, after I restarted the IIS. The PDO function throws the PDOException error with the following message:

could not find driver

For the connection I am using the following function which works pretty well on my local machine:

try {
    $con = new PDO("sqlsrv:Server=" . SERVER . ";Database=" . DATABASE, USERNAME, PASSWORD);
    // set the PDO error mode to exception
    $con->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
} catch (PDOException $e) {
    echo "No connection: " . $e->getMessage();
    exit;
}

What can I else do?

4
  • Do your server have Microsoft Visual C++ 2015 Redistributable ?
    – Eimsas
    Commented Apr 19, 2017 at 8:20
  • @Eimsas Yes. It has.
    – Julian
    Commented Apr 19, 2017 at 8:20
  • 1
    Then try to check logs, it should have more information than "could not find driver" I had same issue with WAMP, but then I just installed MS c++ and tried few versions of php_sqlsrv. Also restart is needed, at least in wamp.
    – Eimsas
    Commented Apr 19, 2017 at 8:23
  • For XAMPP on Windows yo will need the Thread Safe dll's not the NTS ( Not Thread Safe) version
    – RiggsFolly
    Commented Mar 1, 2023 at 13:48

4 Answers 4

15

Here is detailed process if it's helpful for someone. PHP Version - 7.4

  1. Download and extract the .dll files from this link - https://learn.microsoft.com/en-us/sql/connect/php/download-drivers-php-sql-server?view=sql-server-ver15

  2. Paste the files in C:\xampp\php\ext, your path could be different.

  3. in php.ini add those two lines at bottom or in extension section.

     extension=php_sqlsrv_74_ts_x64.dll
     extension=php_pdo_sqlsrv_74_ts_x64.dll
    
  4. Restart your Xampp server, I'll suggest restart your computer and everything will work without an issue then.

Check if SqlSRV enabled

Check using phpinfo() or http://localhost/dashboard/phpinfo.php at like this -

SQLSrv Driver Check in PHP Info

Hope, it will help someone.

7

After I found the error log on the Windows Server, I solved the error by myself.

I got this error in my log:

[21-Apr-2017 07:12:14 UTC] PHP Warning:  PHP Startup: Unable to load dynamic library '...\ext\php_pdo_sqlsrv_7_nts.dll' - %1 is not a valid Win32 application. in Unknown on line 0

Then I downloaded again the driver and installed the x64-Driver. Finally It works without any problems.

2
  • 3
    wish to describe more how to install them? i just download and copy dll to extension folder in php folder named as php version after that restart iis but i cant see it in pdo list in < ? php phpinfo(); ?> Commented May 28, 2019 at 0:24
  • where did you see this error? On which log? How did you enable it? Commented Nov 27, 2020 at 16:10
1

please notice you must use the correct version of php_sqlsrv_xx_xts_xxx.dll and php_pdo_sqlsrv_xx_xts_xx.dll files. enter image description here

for exmple if you use php version 7.4 and a 64 bit system and wamp you must download and use these files: php_sqlsrv_74_ts_x64.dll php_pdo_sqlsrv_74_ts_x64.dll for download you can use this site: https://go.microsoft.com/fwlink/?linkid=2152937 https://learn.microsoft.com/en-us/sql/connect/php/download-drivers-php-sql-server?view=sql-server-ver15

0

It took some time for me to solve the 'No driver'-error. I went through some steps as mentioned here and found some other ones that helped me after new errors. For future references:

  • Download the latest drivers from Microsoft (as said by Julian Schmuckli).
  • Check if your XAMPP is 64 bits(!) with Phpinfo(). If you've got 32-bit, you need different drivers.
  • Add the drivers to your Php.ini file and save the dll's in your php/ext-folder (question of saber tabatabaee yazdi).
  • For the connection, use this code:
$dbh = new PDO ("sqlsrv:Server=$server;Database=$dbname",$username,$pw); 

If you add a port, use:

$server = "192.168.1.15, 51022";

Where the IP (can be hostname to) is your server and 51022 your port.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.