0

I am trying to connect to a SQL Server (MSSQL) database with PHP. I activated the DBO plugin and am trying to use it, but when I define the object and run the code I get the error: Connection failed: could not find driver . As you can see from my code, I have verified that the dbo driver is loaded. I downloaded the sqlsrv driver linked to in one of the answers but I still cannot connect. What am I missing? (the two files index.php and submit.php are in the same directory and that is the whole project) ((I am on a Windows computer, but this may or may not be relevant))

index.php:

<html>
<head>

</head>
<body>
  <form class="my-form" action="submit.php">
      <input type="text" name="field" />
      <input type="submit" value="Submit" />
  </form>
</body>
</html>

submit.php:

<html>
<head>
</head>
<body>
<h1>page loaded</h1>
<h1><?php if (extension_loaded('pdo')) {
    echo 'pdo extension loaded by php';
} ?></h1>

<?php
$myServer = "xxxxxxxxxxxxxxxx";
$myUser = "xxxxxxx";
$myPass = "xxxxxx";
$myDB = "xxxxxxxxxxxx";


$serverName = $myServer; //serverName\instanceName

// Since UID and PWD are not specified in the $connectionInfo array,
// The connection will be attempted using Windows Authentication.
$connectionInfo = array( "Database"=>$myDB, "UID"=>$myUser, "PWD"=>$myPass);
$conn = sqlsrv_connect( $serverName, $connectionInfo);

if( $conn ) {
     echo "Connection established.<br />";
}else{
     echo "Connection could not be established.<br />";
     die( print_r( sqlsrv_errors(), true));
}


?>
</body>
</html>
David
  • 65
  • 2
  • 8
  • Check the server error logs for any error messages. – aynber Oct 07 '19 at 18:49
  • Are you sure you have the `mssql_connect` extension installed on the server? If not, talk to your network administrator to get it installed. – Asrar Oct 07 '19 at 18:52
  • 1
    the php.net article cited by Lars in the answer below says that the mssql extension was removed in php7. and he recommends PDO instead... – David Oct 07 '19 at 19:21
  • Possible duplicate of [PDOException “could not find driver”](https://stackoverflow.com/questions/2852748/pdoexception-could-not-find-driver) – ivan_pozdeev Oct 07 '19 at 21:26
  • @ivan_pozdeev no this is pdo and ms sql, not mysql. – David Oct 08 '19 at 12:45
  • @Ben this doesn't matter. The error is the same, from the same subsystem, so the cause must be the same, just for another driver. – ivan_pozdeev Oct 08 '19 at 13:49
  • @ivan_pozdeev hmm I have tried downloading Microsoft Drivers 5.6 for PHP for SQL Server from the link below but that did not change the error I am getting. Do you know of another place to get the driver? – David Oct 08 '19 at 14:11

1 Answers1

1

The mssql* extension has been removed in PHP 7.

https://www.php.net/manual/en/function.mssql-connect.php

You should consider PDO instead.

edit:

PDO for MSSQL requires the appropriate client libraries, which you will find here: https://learn.microsoft.com/en-us/sql/connect/php/download-drivers-php-sql-server?view=sql-server-2017

Don't let yourself get confused by the version numbers (it's not the PHP version...) check out the description, some drivers are for specific MSSQL server versions only.

Also mind the examples: https://learn.microsoft.com/en-us/sql/connect/php/download-drivers-php-sql-server?view=sql-server-2017

Honk der Hase
  • 2,459
  • 1
  • 14
  • 26
  • Ok yea that functionality is definitely gone, thanks. I updated my question above to show how I have changed my code. – David Oct 07 '19 at 19:44
  • Ok, I've updated my answer as well.. hope it helps ;) – Honk der Hase Oct 07 '19 at 23:03
  • ugg I downloaded the driver mentioned above and it is still not working so I updated my question above. Why am I still unable to connect and getting that error? – David Oct 08 '19 at 14:07
  • unfortunately that did not fix it, I am still getting the same error "driver not found". Maybe I installed the driver wrong? I downloaded the "php_sqlsrv_72_nts_x64" file from the link above, put the dll file with that name into the PHP extensions directory, added `extension=php_sqlsrv_72_nts_x64` to the `[extensions]` section of the ini file and then restarted the computer. The PDO extension is loaded and that fact is verified in the code above. The only 'driver' I installed that I cannot verify with code is that one, so did I do that installation correctly? – David Oct 08 '19 at 18:29
  • https://stackoverflow.com/questions/11246007/pdo-mssql-server-driver-not-found – Honk der Hase Oct 08 '19 at 18:40
  • Have you verified that drivers are loaded in php? use phpinfo() to check – Honk der Hase Oct 08 '19 at 20:53
  • 1. the extension I'm using `php_sqlsrv_72_nts.dll` is already in the format the guy in the other thread is using. 2. I am missing "php_pdo_mssql.dll" I THINK THAT ISTHE PROBLEM where can I download that file from microsoft? My boss says it needs to be a microsoft download. – David Oct 08 '19 at 20:56
  • nevermind I got the file. For me specifically it is called `php_pdo_sqlsrv_7_nts_x86.dll`. To answer your last question I have verified that the pdo plugin is loaded, but I have not verified that the php_pdo_sqlsrv extension is loaded. Do I do `

    ` to verify if that extension is loaded?
    – David Oct 08 '19 at 21:10
  • ok now the php function `sqlsrv_connect` works but gives me a 'NT AUTHORITY\ANONYMOUS LOGON' which I am looking into fixing – David Oct 08 '19 at 21:35
  • Great.. physically it works.. now you'd have to set up the rights properly (which is a differenct topic). – Honk der Hase Oct 08 '19 at 21:37
  • 1
    ok I fixed it and got it to work. I will update my code above. I just did not specify user and pwd in the connection info array, np. Thanks and I marked ur answer as the solution. Thanks for everything! – David Oct 09 '19 at 12:33
  • Glad I could help :) – Honk der Hase Oct 09 '19 at 18:08