I'm trying to get a connection to MS SQL up and running via PHP on my machine. I'm running IIS, have PHP 7.2 installed and MS SQL Express 2017. I have my basic web page running but when I click to open the PHP page, the connection does not work.
session_start();
echo "Hello ";
if (isset($_POST['submit'])) {
$_SESSION["server"] = $_POST['server'];
$_SESSION["database"]= $_POST['database'];
$_SESSION["username"] = $_POST['username'];
$_SESSION["password"] = $_POST['password'];
echo $_SESSION["database"];
//CONNECTION
$serverName = $_SESSION["server"];
$connectionInfo["Database"] = $_SESSION["database"];
$connectionInfo["UID"] = $_SESSION["username"];
$connectionInfo["PWD"] = $_SESSION["password"];
echo "midway";
$conn = sqlsrv_connect($serverName, $connectionInfo);
echo "Bye";
}
When I run this I get "Hello dimensions midway" which suggests the page is working until it hits the connection line.
I am not sure what sqlsrv extension to use.
I have looked around and can see versions for 7 and 7.1 but not for 7.2.
I have added in extension=php_sqlsrv_71_nts_x86.dll
to the bottom of php.ini (and the file exists in C:\Program Files (x86)\PHP\v7.2\ext
).
Any pointers would be gratefully received. I've spent a couple of days on this and don't think I'm getting anywhere.
Thanks
What you need to determine are
Then you must look at the requirement page here
Check the version of PHP you are using, so prefer to your question, it's mean to
PHP 7.2
which SQL Server extension version 5.3 is the best fit.Then go to download page, select the link which specific with version you prefer. (in this question is 5.3)
The file you downloaded will be a self-extractable EXE file, then extract to the place where you want.
you will see list of
dll
files like these...What is the meaning that is in the name of these files?
Like I said before, what you need to determine to select the proper extension file. The SQL Server extension need 2 files of the name start with
php_pdo_sqlsrv_xxx
andphp_sqlsrv_xxx
.Next number mean to your
PHP
version, in this casePHP 7.2
, so you need to choose file name has_72_
in its.Next number mean to your runtime Thread Safe or Non-Thread Safe, which specific in the file name
_ts_
(Thread Safe) or_nts_
(Non-Thread Safe).Next number mean to your runtime 32 bits or 64 bits, which specific in the file name
_x86
(32 bits) or_x64
(64 bits)So check your PHP environment by
phpinfo()
, or run the commandphp --version
on your console, and then notice the message displayed, for my computer it look like these...By look from those information, I got PHP
7.2
,ZTS
mean to Thread Safe (Non-Thread Safe will beNTS
), and thex86
is 32 bits.So, what I must pick is the extension which are
php_sqlsrv_72_ts_x86.dll
andphp_pdo_sqlsrv_72_ts_x86.dll
Final: copy them to your
/php/ext
folder and editphp.ini
with those 2 files name.Then save
php.ini
file and then restart your server.Test your result by looking at
phpinfo.php
, which MUST haspdo_sqlsrv
in the page. That's mean your environment can run PHP and connect toSQL Server
now.I finally found the solution. All you have to do is use these extensions in
php.ini
file.php_sqlsrv_72_ts_x86.dll
php_pdo_sqlsrv_72_ts_x86.dll
and restart your XAMPP.
P.S. "I am using PHP 7.2.5 "
I faced the same issue. Later I found that I was using older version of Microsoft SQL files for PHP.
For PHP 7.2 you have to use 7.2 version of MS PHP SQL
That version is not available through microsoft website. I found it here https://github.com/Microsoft/msphpsql/releases
Once I used those files everything works fine. I used 64 bit files.
Then you need to add these lines in the php.ini file
extension=php_sqlsrv_72_nts.dll
extension=php_pdo_sqlsrv_72_nts.dll
I solved it ~ PHP Version 7.2.4
pdo_sqlsrv : 5.3.0+11108
1.download the proper version sqlsrv and sqlsrv pdo
2.put it into XAMPP\PHP\ext folder
3.after that write the line into php.ini > module setting
extension=php_pdo_sqlsrv_72_ts.dll
extension=php_sqlsrv_72_ts.dll
4.let's make a test for MSSQL still there is a error msg for recommend you to download ODBC Driver for SQL
5.Go to https://docs.microsoft.com/zh-tw/sql/connect/odbc/download-odbc-driver-for-sql-server?view=sql-server-2017 then download the proper file
(mine is Command Line Utilities 11 for SQL Server® X86)
6.fire the test.php again everything works!