Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PDO sqlsrv: could not find driver

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?

like image 273
Julian Schmuckli Avatar asked Apr 19 '17 08:04

Julian Schmuckli


2 Answers

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.

like image 130
Maniruzzaman Akash Avatar answered Oct 20 '22 02:10

Maniruzzaman Akash


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.

like image 43
Julian Schmuckli Avatar answered Oct 20 '22 02:10

Julian Schmuckli