I am trying to connect to a Microsoft SQL server database using PHP 5.6 and am getting the error "could not find driver".
I am initially attempting to use PDO but I could use the procedural version if necessary.
I have downloaded the SQL server PHP driver and placed it in the ext folder in my PHP installation.
I have also specified the extension in php.ini.
My web server is IIS 7.
My platform is Windows 7 Professional (the same box for both the database and the web servers).
PHP is executed correctly by IIS.
I think I must be missing something but can't find out what.
I would appreciate any pointers to what I might have missed or even a tutorial on how to combine the various technologies I have chosen.
I'm sure that someone must have succeeded in doing this.

Dani AI

Generated

Good catch on the thread-safety wrinkle, — and thanks to for the CLI vs web-SAPI checklist. With pdo_sqlsrv visible in phpinfo() the extension is loaded, so the usual next culprits are missing platform components: the Microsoft ODBC driver required by that driver package and the Visual C++ runtime that your PHP build depends on. For the PHP‑5.6 / drivers that target it you’ll typically need the matching ODBC driver (ODBC 11 for the older PHP5 drivers) and the Visual C++ Redistributable that matches your PHP build (VC11 for many PHP 5.6 builds). (learn.microsoft.com)

Confirm the DLL you installed exactly matches the PHP build you’re running: same PHP version, same thread-safety (TS vs NTS) and same bitness (x86 vs x64). Also make sure IIS is loading the php.ini you edited (CLI and IIS can use different ini files). The official driver packages document the exact filenames and loading steps; move the correct DLL into PHP’s extension_dir and restart the web server after edits. (github.com)

Try a minimal PDO test to isolate connection/DSN issues (use exceptions so you see the real error):

try {
  $pdo = new PDO("sqlsrv:Server=127.0.0.1,1433;Database=MyDB", "dbuser", "dbpass");
  $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
} catch (PDOException $e) {
  echo 'Connection failed: ' . $e->getMessage();
}

Note the PDO DSN uses the sqlsrv: prefix and accepts host, port or instance formats. If you get authentication or network errors, the message in the catch will guide the next step. (php.net)

If connection attempts still fail, isolate network/authentication first: test from the web server with sqlcmd or the ODBC Data Source Administrator to confirm you can reach the instance, verify TCP/IP is enabled and the SQL Browser (for named instances) or the proper port is open. For Windows authentication, ensure the IIS app‑pool identity has DB access or switch to SQL auth for testing. These steps let you separate driver/runtime problems from SQL Server/permission problems. (learn.microsoft.com)

Recommended Answers

All 2 Replies

Does running
php -m
on the command line show the moduleas enabled?
How about phpinfo()?
Which module are you using: pdo_mssql or sqlsrv?

I had some issues a few months back with pdo_mssql and but right now I can't remember what I did to fix it. I'll get back to you if I remember.

Hi. Thank you for the replies. I think I have resolved my initial problem... well I've got to the next error message anyway. I did not realise that I had installed PHP 5.6 with a thread-safe dll, which meant that I needed to enable to relevant thread-safe sql server driver for my version of PHP (5.6).
I was side-tracked by reading somewhere that I needed to use the non-thread-safe driver if I was connecting to a Microsoft SQL Server, so I hadn't even attempted to enable the thread-safe drivers in php.
Remember. If you've got PHP5ts.dll in your PHP directory, you should use the two drivers (php_pdo_sqlsrv_56_ts.dll, php_sqlsrv_56_ts.dll).
The pdo_sqlsrv option is now showing as enabled when I use phpinfo().

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.