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
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
What do you need to determine are
Which version of PHP
The runtime you are using is Thread Safe or Non-Thread Safe
The runtime you are using is 64 bits or 32 bits
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...
php_pdo_sqlsrv_7_nts_x64.dll
php_pdo_sqlsrv_7_nts_x86.dll
php_pdo_sqlsrv_7_ts_x64.dll
php_pdo_sqlsrv_7_ts_x86.dll
...
php_sqlsrv_72_ts_x86.dll
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 and php_sqlsrv_xxx.
Next number mean to your PHP version, in this case PHP 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 command php --version on your console, and then notice the message displayed, for my computer it look like these...
PHP 7.2.12 (cli) (built: Nov 8 2018 06:12:12) ( ZTS MSVC15 (Visual C++ 2017) x86 )
Copyright (c) 1997-2018 The PHP Group
Zend Engine v3.2.0, Copyright (c) 1998-2018 Zend Technologies
By look from those information, I got PHP 7.2, ZTS mean to Thread Safe (Non-Thread Safe will be NTS), and the x86 is 32 bits.
So, what I must pick is the extension which are php_sqlsrv_72_ts_x86.dll and php_pdo_sqlsrv_72_ts_x86.dll
Final: copy them to your /php/ext folder and edit php.ini with those 2 files name.
// example with my case
extension=php_pdo_sqlsrv_72_ts_x86
extension=php_sqlsrv_72_ts_x86
Then save php.ini file and then restart your server.
Test your result by looking at phpinfo.php, which MUST has pdo_sqlsrv in the page. That's mean your environment can run PHP and connect to SQL Server now.
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://learn.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!
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 "
How to use SQL Server DLL files with PHP 7.2 version on xampp
https://github.com/Microsoft/msphpsql/releases
To download Windows DLLs for PHP 7.1 or above from the PECL repository, please go to the SQLSRV or PDO_SQLSRV PECL page.
https://pecl.php.net/package/sqlsrv/5.6.1/windows
https://pecl.php.net/package/pdo_sqlsrv/5.6.1/windows
NOTE: Do not forget to remove php_ prefix when you add extension dll filename in php.ini file. eg.
extension=pdo_sqlsrv
extension=sqlsrv
** Do not change actual filename which you put in /xampp/php/ext/ directory.
7.2 Thread Safe (TS) x86 worked for me for sqlsrv and pdo_sqlsrv extensions.
Restart xampp or apache then you can see enabled pdo_sqlsrv extension when you print phpinfo() in a test php file
Related
new php parallel. it's new and there is no troubleshooting about it anywhere and the only documentation about it, is php.net itself which is not enough.
this is what I did (per instructions):
installed latest version of WAMP(3.1.9 x64).
installed latest version of PHP(7.3.9) on WAMP.
added PHP to windows system environment path.
added pthreadVC2.dll to PHP folder & added pthreadVC2.dll to windows system environment path.
added php_parallel.dll to php/ext directory.
added extension=parallel to php.ini.
restarted everything.
<?php
# this is my code
$runtime = new \parallel\Runtime();
$future = $runtime->run(function(){
for ($i = 0; $i < 50; $i++)
echo "$i";
return "easy";
});
for ($i = 0; $i < 50; $i++) {
echo ".";
}
hours spent. now windows cmd says: "Fatal error: Uncaught Error: Class 'parallel\Runtime' not found in ...[my file address]" and wampserver says "The connection was reset (firefox error)" only on the page I use parallel\Runtime and the other pages work fine.
and please with all do respect don't mark my question as broad one or any other things if you simply don't know the answer. at least show me some links.
As far as I can tell, to install an extension from a .dll provided by PECL, the following need to match between your extension and your PHP build, found in phpinfo():
PHP Version: Found in PHP Version
Thread Safe: Found in PHP Extension Build (TS=threadsafe or NTS=not-threadsafe)
Compiler Version: Found in PHP Extension Build (eg: VS15 or VS16)
x86 vs x64: Found in Architecture
On the PECL repository for parallel, you should check latest updated version and the package names are self-explanatory, so you can choose your desired one, i.e. ... 7.3-ts-vc15-x64
Note (maybe does not apply now):
Given the limited builds of parallel, your PHP Build must be: 7.3+, TS, and VC15. Double-check that this is indeed the case for you by looking at your phpinfo(), and also be sure you are using the x64 version (since you said your PHP is x64).
The correct answer is JS_Riddler's post (Dated Dec 3 '19 and edited by T.Todua), but I wanted to elaborate for Windows users.
Before you install, on the command line, if you do "php -v", you may see something like:
PHP 7.3.33 (cli) (built: Nov 16 2021 14:49:44) ( ZTS MSVC15 (Visual C++ 2017) x64 )
Don't let the "(Visual C++ 2017)" confuse you...it's the "ZTS MSVC15" that is important, indicating the thread-safe build and runtime library.
Also note the major and minor version numbers: "7.3" in the above example. The build number (eg the third number, '33' in the example) does not matter.
And also note the architecture: "x64" in the example.
The major version, minor version, and architecture need to match the version of parallel you download, so for the above example you would want to download
php_parallel-1.1.4-7.3-ts-vc15-x64.zip
Note: after downloading and unzipping the parallel archive:
copy the php_parallel.dll file to php/ext/php_parallel.dll
copy the pthreadsVC2.dll to php/pthreadsVC2.dll
add "extension=parallel" somewhere in your php.ini on a line by itself.
you should be able to run "php -v" from a command line. If you get an error, double check that the php.exe (and pthreadsVC2.dll) are on your PATH and that the php_parallel.dll version you have in the php/ext folder matches the major and minor versions of your php executable.
Another note: Many hosting companies do not use the ZTS version of php, so it appears this extension is mostly for CLI development applications at this time. Don't try to write production code that relies on it unless you know your target host supports it.
Error log turned out the saver.
PHP Startup: Failed to load .../php/php7.4.9/ext/pthreadVC2, The system cannot find the file ..
Then I've just removed .dll from file name, and then it gave:
PHP Startup: Invalid library (maybe not a PHP library) 'pthreadVC2' in Unknown on line 0
If I remember correctly, it turned out that WampServer (with it's builtin php) does not support multi-threading/safe-threading or like that, and it was not possible to use it on WampServer. However interesting thing turned out, is that it works in CLI.
I had the exact same issue. Also, I copied the file pthreadVC2.dll into the folder ext. When I moved this file out of the folder then it has worked fine.
Folders:
yourPhpFolder/pthreadVC2.dll
yourPhpFolder/ext/php_parallel.dll
I have been using a software which files are encoded by Zend Guard. Since PHP 5.3 + versions don't support Zend Optimizer hence I have to install Zend Guard Loader to run that application on my windows xampp installation.
I downloaded Zend guard loader and copied to xampp folder.Then I edited the following lines to php.ini file.
zend_extension ="E:\xampp\ZendGuardLoader\php-5.4.x\ZendLoader.dll"
zend_loader.enable=1
Then restarted the xampp but it was giving php5.dll missing error then I downloaded php5.dll file from internet and uploaded to xampp/php folder and edited following file
xampp\apache\conf\extra\httpd-xampp.conf here I edited this line
LoadFile "/xampp/php/php5ts.dll"
to
LoadFile "/xampp/php/php5.dll"
then the php5.dll error gone and the following error occurred:
"the procedure entry point zend_new_interned_string could not be located in the dynamic link library E:\xampp\ZendGuardLoader\php-5.4.x\ZendLoader.dll"
When I tried to search on internet then found that this is the issue with 'Thread Safety' is enabled in xampp php. I don't know how to disable 'Thread Safety' or how to use 'Non Thread Safety' php version with xampp.
Please help me to install Zend Guard Loader on Windows 8 os Xampp 1.8.1 with php 5.4.x
Before you waste any more time checking, see if phpinfo() output shows that Thread Safety is enabled or not.
Zend Extensions are NTS (non thread safe) so if it is enabled, it cannot load and you need NTS PHP to load Guard Loader extension.
And
Thread Safety cannot be disabled, it is a compilation flag and how the compiled binary works.
You need to load a different Apache PHP Module which is non-thread-safe (php.net releases both TS and NTS builds).
Take a look to
http://forums.zend.com/viewtopic.php?f=57&t=43013
So, I had this same issue.
XAMPP (as far as version 3.2.1) comes bundled with Thread Safe PHP compilation. This is php binaries are compiled using a flag to force "Tread Safe", so, it can't be switched back on the same binaries.
You need to change the binaries of XAMP for those Non Tread Safe (NTS) compiled.
I followed this guide for replacing PHP in XAMP to use NTS. The guide explains how to enable FastCGI and Zend OPcache, so if you copy paste the steps, you should have XAMP run PHP on FastCGI mode. The relevant steps are:
Identify the version of Apache server that is running. [..]
Download the latest PHP binaries for windows. Choose the non-thread-safe (you might choose a newer version of php) [...] Create a new folder for the new version of PHP (ex. c:\xampp\php56-nts ).
Edit the Apache configuration file (ex: c:\xampp\apache\apache\conf\extra\ httpd-xampp.conf ) and replace the paths in the php5 module configuration you may need to change the name of the .dll
As php binaries downloaded from php.net can be used for developpment or for production, you need to create a php.ini according to your needs (there are 2 templates in the folder you can use to begin with: php.ini-development and php.ini-production )
With this, basically you have XAMPP to run PHP's NTS version. Afther that, you can install Zend Guard Loader as the guide says. Basically:
Download the Zend Guard Loader binary
Locate the ZendLoader.dll file and paste it inside the library path of php for xampp. Get the path.
Configure php.ini to use ZendLoader; Add
zend_extension=< full_path_to_ZendLoader.dll>
zend_extension=< full_path_to_opcache.dll>
Add any other optional configuration. Restart Apache and it should restart with the NTS version of PHP. You can test this with localhost/xampp/phpinfo.php
I'm trying to load the MongoDb extension for php on a Windows 7 64 bit PC, running XAMPP with Apache 2.0 and PHP 5.3.5, compiler version VC6. I started out with this tutorial, and have also tried the extensions that the php documentation suggested. MongoDb itself is running fine, and shell commands seem to work.
At the moment I have:
Added extension=php_mongo.dll to my php.ini file.
Put the VC6 version of php_mongo.dll (from the following download page, suggested by the PHP documentation) in the php/ext folder.
Restarted my Apache server.
I have also tried php_mongo.dll files form Stealth's github, but they didn't seem to work either.
When restarting Apache, I don't get any errors, but I can't find anything on MongoDb in phpinfo(), and the Mongo class is not recognised either.
Any ideas of what might be wrong? Let me know if you need more info.
The problem might be incorrect version of extension. Try different versions downloaded from here: http://pecl.php.net/package/mongo/1.6.4/windows
Use Bitnami WAMP Stack instead of XAMPP it comes preloaded php_mongo.dll driver installed and no need to configure php.ini file
There is no official MongoDB Driver released for PHP7. So it would be better to download the recommended PHP version 5.5.30(currently) offered by Bitnami Wamp Stack.
There might be two reasons:
First one is as #Sadd suggested. Your mongodb extension should be loaded after what you have done so its very likely that you have enabled extension in wrong file. And yes, there are two such similar files (named php.ini-development and php.ini-production on windows computers), so make sure to enable it in php.ini file and run the following code echo extension_loaded("mongodb") ? "loaded\n" : "not loaded\n"; and you should see loaded on screen.
If you still don't see it, then the second thing you can do is to check whether the version is right or not. You can check your phpinfo() for your php version and then put the right versioned dll file in ext folder. In this, you can have to check whether your php is ts or nts and second one is whether your php is x64 or x86. I hope you are good to go after one or both of the changes.
This was driving my crazy until I looked closely at my PHP version:
$ php -v
PHP 7.4.1 (cli) (built: Dec 17 2019 19:23:59) ( NTS Visual C++ 2017 x64 )
Notice that it says PHP 7.4.1 and NTS and x64? This means that when I download from PECL, I need version 7.4 Non Thread Safe (NTS) x64 or it won't run correctly.
If the correct version of module is not added, an error is thrown.
[PHP Warning: PHP Startup: Unable to load dynamic library 'php_mongodb.dll' (tried: C:\xampp\php\ext\php_mongodb.dll (The specified module could not be found), C:\xampp\php\ext\php_php_mongodb.dll.dll (The specified module could not be found)) in Unknown on line 0]
When downloading the dll extension from https://pecl.php.net/package/mongodb, make sure that NTS (Non Thread Safe) or TS (Thread Safe) is chosen according to what shown from php -v command.
$ php -v
PHP 8.1.7 (cli) (built: Jun 7 2022 21:45:53) (ZTS Visual C++ 2019 x64)
Although here it shows ZTS, TS version can be used in this case.
I upgraded to PHP 5.3, and noticed that php_mssql.dll is missing. After googling a bit, it seems that 5.3 has dropped support for mssql. So I downloaded the drivers from microsoft, but I can't seem to get it to work.
I am on windows. What do I need to do, after I unzip the files in the ext directory of my PHP 5.3?
Quoting http://php.net/manual/en/intro.mssql.php:
The MSSQL extension is not available anymore on Windows with PHP 5.3 or later.
SQLSRV, an alternative driver for MS SQL is available from Microsoft: » http://msdn.microsoft.com/en-us/sqlserver/ff657782.aspx.
Once you downloaded that, follow the instructions at this page:
Loading the Microsoft Drivers for PHP for SQL Server
In a nutshell:
Put the driver file in your PHP extension directory.
Modify the php.ini file to include the driver. For example:
extension=php_sqlsrv_53_nts_vc9.dll
Restart the Web server.
See Also (copied from that page)
System Requirements (Microsoft Drivers for PHP for SQL Server)
Getting Started
Programming Guide
SQLSRV Driver API Reference (Microsoft Drivers for PHP for SQL Server)
The PHP Manual for the SQLSRV extension is located at http://php.net/manual/en/sqlsrv.installation.php and offers the following for Installation:
The SQLSRV extension is enabled by adding appropriate DLL file to your PHP extension directory and the corresponding entry to the php.ini file. The SQLSRV download comes with several driver files. Which driver file you use will depend on 3 factors: the PHP version you are using, whether you are using thread-safe or non-thread-safe PHP, and whether your PHP installation was compiled with the VC6 or VC9 compiler. For example, if you are running PHP 5.3, you are using non-thread-safe PHP, and your PHP installation was compiled with the VC9 compiler, you should use the php_sqlsrv_53_nts_vc9.dll file. (You should use a non-thread-safe version compiled with the VC9 compiler if you are using IIS as your web server). If you are running PHP 5.2, you are using thread-safe PHP, and your PHP installation was compiled with the VC6 compiler, you should use the php_sqlsrv_52_ts_vc6.dll file.
The drivers can also be used with PDO.
Download Microsoft Drivers for PHP for SQL Server. Extract the files and use one of:
File Thread Safe VC Bulid
php_sqlsrv_53_nts_vc6.dll No VC6
php_sqlsrv_53_nts_vc9.dll No VC9
php_sqlsrv_53_ts_vc6.dll Yes VC6
php_sqlsrv_53_ts_vc9.dll Yes VC9
You can see the Thread Safety status in phpinfo().
Add the correct file to your ext directory and the following line to your php.ini:
extension=php_sqlsrv_53_*_vc*.dll
Use the filename of the file you used.
As Gordon already posted this is the new Extension from Microsoft and uses the sqlsrv_* API instead of mssql_*
Update:
On Linux you do not have the requisite drivers and neither the SQLSERV Extension.
Look at Connect to MS SQL Server from PHP on Linux? for a discussion on this.
In short you need to install FreeTDS and YES you need to use mssql_* functions on linux. see update 2
To simplify things in the long run I would recommend creating a wrapper class with requisite functions which use the appropriate API (sqlsrv_* or mssql_*) based on which extension is loaded.
Update 2:
You do not need to use mssql_* functions on linux. You can connect to an ms sql server using PDO + ODBC + FreeTDS. On windows, the best performing method to connect is via PDO + ODBC + SQL Native Client since the PDO + SQLSRV driver can be incredibly slow.
Make sure to download the SQL driver here https://learn.microsoft.com/en-us/sql/connect/php/download-drivers-php-sql-server?view=sql-server-ver15
Then move in the two files with the same version as your php version. (check php -v)
In my case I was using version 7.4.30 and moved the php_pdo_sqlsrv_74_nts_x64.dll and
php_sqlsrv_74_nts_x64.dll into my php folder ext
C:\Program Files\php7.4.30\ext
Then open your php.ini file and add the extensions, remember to include ".dll
" on the extensions.
extension=php_pdo_sqlsrv_74_nts_x64.dll
extension=php_sqlsrv_74_nts_x64.dll
If you still have problems go to extension dir = "ext" in php.ini and replace ext with the exact path to your ext folder like this:
extension_dir = "C:\Program Files\php7.4.30\ext\"
I recently installed IIS, PHP 5.3.5 and MySQL, and had it all working.
I then has a requirement for MS SQL as well, and what I thought would be an easy task, is now driving me crazy and I can't figure out how to make it work.
According to php_info() the MS SQL drivers aren't loading.
I downloaded php_mssql.dll and enabled it in php.ini but it didn't work. I found out that the new version of PHP uses a different dll. So I downloaded the new one, put that in the ext folder, and updated the php.ini, but still no luck.
php_info() still isn't showing the drivers are loaded.
I am about to uninstall IIS and PHP, and install Apache, which I've installed before.
This question is my last attempt, can anyone help?
This is a snippet from a customer FAQ I wrote up a wee while ago to help our dedicated hosting customers get PHP up and running with the MS SQL Drivers. It may duplicate some knowledge already imparted in the comments above, but it's completeness may also help others starting from scratch:
Note: Clearly the PHP version numbers have moved on a bit since I wrote this but the general principles still apply.
Installation Pre-requisite:
The SQL Native Client is required for the Microsoft Drivers for PHP for SQL Server:
Microsoft SQL Server 2008 R2 Native Client X64 - for 64 bit Windows
or
Microsoft SQL Server 2008 R2 Native Client X32 - for 32 bit Windows
Download and install the Native Client driver that matches your system.
PHP MS SQL Drivers:
Download the Microsoft Drivers for PHP for SQL Server:
Microsoft Drivers for PHP for SQL Server
The file is a self-extracting executable so just use 7zip or WinRAR and extract the files to a folder of your choice. We now need to decide which driver to choose that matches your PHP installation.
PHP 5.3.5 comes in four different flavours:
PHP 5.3.5 Non-threadsafe VC9
PHP 5.3.5 Non-threadsafe VC6
PHP 5.3.5 Threadsafe VC9
PHP 5.3.5 Threadsafe VC6
To tell which version you have installed open the snapshot.txt file which lives in the same folder as php.exe, php-cgi.exe etc. Don't use notepad.exe because the file uses just line feed (\n) for line ends (unix format) at the start and notepad mangles these into a single line.
In the file you will see a line (around line 6, or near the middle of line 1 of you did use notepad) starting with: Build::
Build: D:\php-sdk\snap_5_3\vc9\x86\obj\Release - non-threadsafe, VC9 build
Build: D:\php-sdk\snap_5_3\vc6\x86\obj\Release - non-threadsafe, VC6 build
Build: D:\php-sdk\snap_5_3\vc9\x86\obj\Release_TS - threadsafe, VC9 build
Build: D:\php-sdk\snap_5_3\vc6\x86\obj\Release_TS - threadsafe, VC6 build
This tells us what version of PHP you're running.
From the folder you extracted the MS SQL PHP drivers, choose the driver(s) that matches the version of PHP being used (if you're not using PDO then you don't need to copy the php_pdo_ drivers):
PHP 5.3.5 Non-threadsafe VC9: php_sqlsrv_53_nts_vc9.dll, php_pdo_sqlsrv_53_nts_vc9.dll
PHP 5.3.5 Non-threadsafe VC6: php_sqlsrv_53_nts_vc6.dll, php_pdo_sqlsrv_53_nts_vc6.dll
PHP 5.3.5 Threadsafe VC9: php_sqlsrv_53_ts_vc9.dll, php_pdo_sqlsrv_53_ts_vc9.dll
PHP 5.3.5 Threadsafe VC6: php_sqlsrv_53_ts_vc6.dll, php_pdo_sqlsrv_53_ts_vc6.dll
Assuming your PHP installation lives in C:\PHP, copy (DON'T MOVE) these files to your C:\PHP\EXT folder.
Open C:\PHP\PHP.INI and locate the Dynamic Extensions part of the file and add:
extension=php_sqlsrv_53_nts_vc9.dll
extension=php_pdo_sqlsrv_53_nts_vc9.dll <-- optional
Finally, to be sure that PHP can find these extensions, ensure the extension_dir directive in C:\PHP\PHP.INI is set to C:\PHP\EXT:
extension_dir = C:\PHP\ext
Restart IIS and call phpinfo(). If all is well you should see:
And if you also loaded the PDO driver you should see:
The above mentioned:
extension=php_sqlsrv_53_nts_vc9.dll
extension=php_pdo_sqlsrv_53_nts_vc9.dll
what I found is incorrect.
Therefore, it is likely to be:
extension=php_sqlsrv_53_ts_vc9.dll
extension=php_pdo_sqlsrv_53_ts_vc9.dll