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=10.6.2.105;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 http://php.net/manual/en/pdo.installation.php 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:

extension=php_pdo_mysql.dll
extension=php_pdo_oci.dll
extension=php_pdo_sqlite.dll

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?

Details:

PHP 7.0.5
Windows 8.1
NTS x86

 Answers

5

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

https://github.com/Microsoft/msphpsql/releases

php.ini line added:

extension=php_pdo_sqlsrv_7_nts.dll
Thursday, April 1, 2021
 
njai
 
2

If you are using a stored procedure then use

SET NOCOUNT ON 

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
 
3

For some reason this works:

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

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

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
 
Matt
 
3

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

extension=php_pdo_mysql.dll

On Linux you install the extension with the package manager:

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

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 https://www.microsoft.com/en-us/download/details.aspx?id=56567 (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 https://docs.microsoft.com/en-us/sql/connect/php/system-requirements-for-the-php-sql-driver?view=sql-server-2017

More info can be found at https://docs.microsoft.com/en-us/sql/connect/php/loading-the-php-sql-driver?view=sql-server-2017 and https://docs.microsoft.com/en-us/sql/connect/php/system-requirements-for-the-php-sql-driver?view=sql-server-2017

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

PDO connection test:

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

sqlsrv_connect connection test:

$SqlServer = "THISSERVERSQLEXPRESS";
$DbConnInfo = array( "Database"=>"TheDatabase", "UID"=>"DbUName", "PWD"=>"DbPassword");
$SqlServerCon = sqlsrv_connect( $SqlServer, $DbConnInfo);
if( $SqlServerCon ) {echo "Connection established";}
else
{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 :