Reputation: 173
I've been trying to establish a connection between a PHP script and a Microsoft Azure SQL database but keep running into this same issue. I've tried Xampp version 7.1.11 / PHP 7.1.11, 7.0.25 / PHP 7.0.25
, and now I am using 5.6.32 / PHP 5.6.32.
I've installed the Microsoft Drivers for PHP for Microsoft SQL Server and am using the SQLSRV32.EXE
for Version 5.6.32 which is added to the C:\xampp\php\ext
folder.
When I edit the php.ini
file to add extension=php_sqlsrv_56_nts.dll
save and reboot apache in the php error log file I keep getting the error
PHP Warning: PHP Startup: Unable to load dynamic library 'C:\xampp\php\ext\php_sqlsrv_56_nts.dll' - The specified module could not be found. in Unknown on line 0
Each version of XAMPP that I've tried has done this. Can anyone lend an idea or a hand how I can get around this and complete my SQL connection using PHP to an Azure SQL database?
Upvotes: 0
Views: 1768
Reputation: 173
So after further tinkering and research, I've finally solved the issue I've mentioned above. Unfortunately, the common google search provides this Microsoft Drivers for PHP for SQL Server which only contains drivers up to PHP Version 7.0.XX. For PHP Version 7.1.12
I've used the thread safe1
php_sqlsrv_71_ts_x64.dll
found here Microsoft Drivers 4.3 for PHP for SQL Server. I'm not sure why it took me another three days to find this.
The connection to the Azure SQL Database now works, the steps are mentioned below.
1. Acquire the correct SQL driver from Microsoft
-If you are using PHP Version 7.0 or earlier the drivers can be found Here
-If you are using PHP Version 7.1 the drivers can be found Here
2. Install Microsoft ODBC Driver 13 for SQL Server
3. Install required .dll's
to the php\ext
folder. eg. C:\php\ext
4. Edit the php.ini
file and ensure the extension_dir=
points to the C:\php\ext
folder that you placed the downloaded .dll's
(drivers) in.
5. Add extension=required .dll name
to the php.ini file approximately line 890. In my case this is extension=php_sqlsrv_71_ts_x64.dll
6. Save php.ini
and restart Apache
.
The PHP script I've used to test my connection is as follows:
sqltest.php
$connectionInfo = array("UID" => "USERNAME", "pwd" => "PASSWORD", "Database" => "DBNAME", "LoginTimeout" => 30, "Encrypt" => 1, "TrustServerCertificate" => 0);
$serverName = "SERVERNAME";
$conn = sqlsrv_connect($serverName, $connectionInfo);
if (sqlsrv_errors($conn)) {
die('Failed to connect to Azure SQL: '.sqlsrv_errors());
} else {
echo "Connection to Microsoft Azure SQL Server has succeeded! <br /><br />";
}
$tsql = "SELECT * FROM [Users];";
$getResults = sqlsrv_query($conn, $tsql);
echo ("Reading data from table <br />" . PHP_EOL);
if ($getResults == FALSE)
echo(sqlsrv_errors());
while ($row = sqlsrv_fetch_array($getResults, SQLSRV_FETCH_ASSOC)) {
echo ($row['Id'] . " " . $row['Name'] . " " . $row['Username'] . " " . $row['Password'] . "<br /> " . PHP_EOL);
}
sqlsrv_free_stmt($getResults);
?>
Further information is provided by Microsoft Here
Thanks again for all of the helpful input from everyone above.
Upvotes: 1
Reputation: 9950
First, open a phpinfo()
and search for the line Thread safety. If it says enabled, then you should use the Thread-Safe DLLs instead.
Upvotes: 1