Asked  1 Year ago    Answers:  5   Viewed   11 times

I have a simple script which works in PHP 5.6 but does not work in PHP 7.0:

$conn = new PDO("sqlsrv:Server=;Database=mydb", "myuname", "mypwd");
$sql = "SELECT TOP 3 txt FROM mytab";
foreach ($conn->query($sql) as $row)
    print $row["txt"] . "rn";

When I run it in PHP 7.0, I get the following error:

Uncaught PDOException: could not find driver

According to I don't need extension=php_pdo.dll in my php.ini file in versions above 5.3 for windows installs.

I do have the following though:


Also, I can use sql server directly, just not with PDO.

What else do I need to get this working? Where do I find the driver for this?


PHP 7.0.5
Windows 8.1
NTS x86



Had already downloaded the driver and it didn't work. Found a new site for the driver and this one works.

php.ini line added:

Thursday, April 1, 2021

If you are using a stored procedure then use


The problem is that the stored procedure returns a result containing the number of rows affected as the first result.

Microsoft Documentation

Thursday, April 1, 2021

For some reason this works:

  $sth = $dbh->prepare("exec wcweb_UserInfo ?");
  $sth->bindParam(1, $name);

  while($result = $sth->fetch(PDO::FETCH_ASSOC)) {

I might be able to live with this. Anyone know why the other methods do not work? Is it a difference in the libraries?

Saturday, July 31, 2021

To use different drivers you need to install them. On Windows you simply uncomment a line in php.ini:


On Linux you install the extension with the package manager:

sudo apt install php7.1-mysql
Friday, August 6, 2021

I got it figured out. I had to install the ODBC Driver 17 for SQL Server (msodbcsql_17.2.0.1_x64.msi) on my server. The SQL Server Native Client 11.0 was installed but not the ODBC Driver for SQL Server.

For future reference for anyone else with this or a similar issue...

It can be downloaded at (note: if you have a 32 bit server, you will want to install the msodbcsql_17.2.0.1_x86.msi - If you accidentally try to install the incorrect version, it will let you know during the installation). After the driver is installed, you need to reboot the server. It won't prompt you to restart, but you'll need to.

In my PHP.ini I have added extension=php_pdo_sqlsrv_72_nts.dll and extension=php_sqlsrv_72_nts_x64.dll They can be downloaded at

More info can be found at and

I can now establish a connection to Sql Server using either sqlsrv_connect or PDO.

PDO connection test:

$SqlServerCon = new PDO("sqlsrv:server=$SqlServer;Database=TheDatabase", "DbUName", "DbPassword"); 
if (!$SqlServerCon) {die('Unable To Connect to Sql Server');}
{echo "Connection Successful";} 

sqlsrv_connect connection test:

$DbConnInfo = array( "Database"=>"TheDatabase", "UID"=>"DbUName", "PWD"=>"DbPassword");
$SqlServerCon = sqlsrv_connect( $SqlServer, $DbConnInfo);
if( $SqlServerCon ) {echo "Connection established";}
{echo "Connection could not be established.<br />";
die( print_r( sqlsrv_errors(), true));}
Wednesday, November 3, 2021
Only authorized users can answer the question. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :