There are questions on Microsoft's SQL Server driver for PHP forum from people that cannot work out how to install the PHP driver.
PHP offers some help on choosing extensions here: http://php.net/manual/en/install.pecl.windows.php, and here: http://php.net/manual/en/install.windows.extensions.php.
It's still a bit complicated, so here are some explicit instructions which I hope can help new users of PHP. After a while all of this will become common sense.
Instruction notes:
- Microsoft's PHP driver for SQL Server only runs with PHP 32-bit builds (as standard from http://windows.php.net/download).
- These instructions are only for PHP 5.2, 5.3 or 5.4. (5.5 and 5.6 supported with custom build.)
- I am pretending that PHP is installed into folder C:\PHP. Replace the term C:\PHP in the instructions with whatever folder your PHP is installed into.
- See orange text for PHP 5.5 and 5.6 support in 32 and 64 bits. If you do not know whether you have 64-bit PHP 5.5/5.6, start the instructions at step 5, note the orange comment in 5.3, then return to step 1 of the instructions below.
- Download Microsoft's PHP drivers. Open page: http://www.microsoft.com/en-us/download/details.aspx?id=20098
- If you have Windows Vista, Server 2008 or above, download SQLSRV30.EXE.
- If you have Server 2003/Windows XP or below, download SQLSRV20.EXE
- If you have PHP 5.5/5.6, or you have Windows XP/2003 and you want to install driver 3.0...
- See this post: Unofficial php_sqlsrv 3.0 and download the archive
- Note the text in orange in step (2.) below - you don't need to extract the 'source code' folder. Also...
- If you have 64-bit PHP, you need to extract only the files in sub-folder x64 of the zip file, and can ignore everything else.
- If you have 32-bit PHP, you should ignore the folder x64 in the zip file.
- Assume for step (6.) below that you downloaded SQLSRV30.EXE
- Remember that PHP 5.5+ does not work on Windows XP/2003 - for this OS you can only go up to PHP 5.4.
- Extract the entire contents of the file downloaded in (1.) to your PHP extensions folder. This is usually a sub-folder of the location where php is installed, named 'ext', e.g. C:\PHP\ext.
Note that you can use WinRAR to open the .exe file and extract the files, if you want.
For users of the Unofficial php_sqlsrv 3.0 zip file: - If you have 64-bit PHP, please extract only the contents of folder x64 of the zip file.
- If you have 32-bit PHP, extract the files in the root of the zip and ignore any folders.
- Download the SQL Server Native Client (SNAC), the PHP driver depends on it:
- For SQLSRV30.EXE (Windows Vista and above), go here: http://www.microsoft.com/en-us/download/details.aspx?id=29065#SNAC. These are direct links:
- SQL Server 2012 Native Client for 64 Bit Windows (x64)
- SQL Server 2012 Native Client for 32 Bit Windows (x86)
- For SQLSRV20.EXE (Windows XP/2003), go here: http://www.microsoft.com/en-us/download/details.aspx?id=16978#SNAC. Here are the direct links:
-
Install the SQL Server Native Client downloaded from the previous step (3.) on the same computer that runs PHP.
- Find out which version of PHP you have (if you already know it, go to step 6):
- Open a command window (run cmd.exe)
- Using your PHP folder, run command: "C:\PHP\php.exe" -i | more
'more' loads the information page by page. - The top line will show PHP Version => 5.x.x... Make a note of the version. It will start with 5.6, 5.5, 5.4, 5.3 or 5.2. The final (3rd) number is not important.PHP 5.5+ users can also tell if they have a 64-bit or 32-bit version of PHP by looking at the reported Architecture: x86 = 32-bit and x64 = 64-bit.For the next steps you may need to scroll to the 2nd information page in the console window, by pressing the space bar...
- For version 5.6, 5.5, 5.4 and 5.3, a little further down is setting PHP Extension Build => API... It will end with either ...TS,VCn or ...NTS,VCn. Make a note of whether it says TS or NTS. Also make a note of whether the final part is VC11, VC9 or VC6 (VC6 is still possible with PHP 5.3 and SQLSRV20.EXE).
- For PHP version 5.2 only: note the setting that says "Thread Safety enabled" or "Thread Safety disabled".
"Thread Safety enabled" means TS,VC6
"Thread Safety disabled" means NTS,VC6.
- Edit your php configuration file, php.ini. Go to the end of the file and add these lines, depending on your earlier choices. You need to know your PHP version number (5.2, 5.3, 5.4, 5.5 or 5.6), and your TS/NTS setting, and whether you have a VC11, VC9 or VC6 build, all noted from step 5. above.
You can calculate the files you need by looking at the file name and comparing to your PHP version, and remembering whether you downloaded SQLSRV20.exe or SQLSRV30.exe. Here are all of the combinations that are possible:
-
For the custom drivers, PHP 5.6, NTS, VC11 add lines:
extension=php_sqlsrv_56_nts.dll
extension=php_pdo_sqlsrv_56_nts.dll -
For the custom drivers, PHP 5.6, TS, VC11 add lines:
extension=php_sqlsrv_56_ts.dll
extension=php_pdo_sqlsrv_56_ts.dll -
For the custom drivers, PHP 5.5, NTS, VC11 add lines:
extension=php_sqlsrv_55_nts.dll
extension=php_pdo_sqlsrv_55_nts.dll -
For the custom drivers, PHP 5.5, TS, VC11 add lines:
extension=php_sqlsrv_55_ts.dll
extension=php_pdo_sqlsrv_55_ts.dll -
For SQLSRV30.EXE (or the custom drivers), PHP 5.4, NTS, VC9 add lines:
extension=php_sqlsrv_54_nts.dll
extension=php_pdo_sqlsrv_54_nts.dll -
For SQLSRV30.EXE (or the custom drivers), PHP 5.4, TS, VC9 add lines:
extension=php_sqlsrv_54_ts.dll
extension=php_pdo_sqlsrv_54_ts.dll -
For SQLSRV30.EXE (or the custom drivers), PHP 5.3, NTS, VC9 add lines:
extension=php_sqlsrv_53_nts.dll
extension=php_pdo_sqlsrv_53_nts.dll -
For SQLSRV30.EXE (or the custom drivers), PHP 5.3, TS, VC9 add lines:
extension=php_sqlsrv_53_ts.dll
extension=php_pdo_sqlsrv_53_ts.dll -
For SQLSRV20.EXE, PHP 5.3, NTS, VC9 add lines:
extension=php_sqlsrv_53_nts_vc9.dll
extension=php_pdo_sqlsrv_53_nts_vc9.dll -
For SQLSRV20.EXE, PHP 5.3, NTS, VC6 add lines:
extension=php_sqlsrv_53_nts_vc6.dll
extension=php_pdo_sqlsrv_53_nts_vc6.dll -
For SQLSRV20.EXE, PHP 5.3, TS, VC9 add lines:
extension=php_sqlsrv_53_ts_vc9.dll
extension=php_pdo_sqlsrv_53_ts_vc9.dll -
For SQLSRV20.EXE, PHP 5.3, TS, VC6 add lines:
extension=php_sqlsrv_53_ts_vc6.dll
extension=php_pdo_sqlsrv_53_ts_vc6.dll -
For SQLSRV20.EXE, PHP 5.2, NTS, VC6 add lines:
extension=php_sqlsrv_52_nts_vc6.dll
extension=php_pdo_sqlsrv_52_nts_vc6.dll -
For SQLSRV20.EXE, PHP 5.2, TS, VC6 add lines:
extension=php_sqlsrv_52_ts_vc6.dll
extension=php_pdo_sqlsrv_52_ts_vc6.dll
-
For the custom drivers, PHP 5.6, NTS, VC11 add lines:
- Restart your web server for the change to take effect. That means stop and start IIS, Apache, or whatever you use, not the whole computer.
Regarding Step 6. Is there no combination that will work for Server 2003/IIS6 (SQLSRV20.EXE) and PHP 5.4.x?
ReplyDeleteHi Bryan
DeleteIf you go back to step 1.3 (orange writing), you can use a custom build of driver version 3.0 with PHP 5.4 and Windows 2003. It's the only way for that combination - it should work fine as long as you stick to the printed functionality from SQLSRV20.EXE. I just changed a couple of lines so that it used the ODBC driver for 2008 R2 instead of 2012 (which only supports Win Vista+).
Thank you so much.. It works! :)
ReplyDeleteHi Rob!! I did everything you said, but still cant have my connection working!!!! I have wamp server 32 bits running on 64 bits windows 7, I got this information from the php.exe
ReplyDeletePHP 5.4, TS, VC9 so i added the lines you told me to add!! I have this code as well
$server='ANDRES-PC';
echo "got server name";
$connectinfo=array("Database"=>"ejemplo");
//connect to DB
echo "got db name";
$db=sqlsrv_connect($server,$connectinfo);
echo "got connection";
if($db)
{
echo "Connection established.
";
}
else
{
echo "Connection couldn't be established.
";
die(print_r( sqlsrv_errors(), true));
}
When I execute the page I see the first two echo lines I wrote, but after this line $db=sqlsrv_connect($server,$connectinfo); nothing happens.
I wrote the php.ini line with the other extension lines, does it makes any difference?
thanks for your help!
Hi Alvaro
DeleteHave you installed SQL Server Native Client 2012 ?
Try to find your PHP log file and see what error is reported...
Okay.....I did it!!! Thanks Rob this blog is really helpful!!! The problem was that for some reason the php.ini file wasn't being updated so I opened the php.ini file form the wamp server logo and I saw that the lines you told me to add were missing.I wrote then and the connection is finally working!!
ReplyDeleteHello Rob,
ReplyDeletethe last days I tried to set up a connection a MSSQL Server without any success. Today I found your blog and I read every step you did and realized i did the same. But maybe I made a careless mistake. Here some info about my config:
- I am working on a Windows 7 64 bit machine
- PHP version is : 5.4.8 nts (dir is : D:\Development\PHP\PHPcore\)
- downloaded the SQLSRV30.EXE put the files in my ext folder of PHP (D:\development\PHP\PHPcore\ext)
- added the following lines to my php.ini:
extension=php_sqlsrv_54_nts.dll
extension=php_pdo_sqlsrv_54_nts.dll
(this should work cause i can access the following function without getting and unknown function error : sqlsrv_connect)
- downloaded the SQL Server Native Client in my case the 64 bit version and installed it (could not change the install path or anything)
And here is the problem : When i run the sqlsrv_connect function and check if it is set up propperly with print_r( sqlsrv_errors(), true) I get following error:
( [0] => Array ( [0] => IMSSP [SQLSTATE] => IMSSP [1] => -49 [code] => -49 [2] => This extension requires the Microsoft SQL Server 2012 Native Client. Access the following URL to download the Microsoft SQL Server 2012 Native Client ODBC driver for x86: http://go.microsoft.com/fwlink/?LinkId=163712 [message] => This extension requires the Microsoft SQL Server 2012 Native Client. Access the following URL to download the Microsoft SQL Server 2012 Native Client ODBC driver for x86: http://go.microsoft.com/fwlink/?LinkId=163712 ) [1] => Array ( [0] => IM002 [SQLSTATE] => IM002 [1] => 0 [code] => 0 [2] => [Microsoft][ODBC Driver Manager] Der Datenquellenname wurde nicht gefunden, und es wurde kein Standardtreiber angegeben [message] => [Microsoft][ODBC Driver Manager] Der Datenquellenname wurde nicht gefunden, und es wurde kein Standardtreiber angegeben ) )
You can ignore the german part.
Can you see a mistake here ? Did i forget anything ? I hope you can help me.
Thanky you in adavance
Aleks
Hi Aleks
DeleteThe error is saying that the ODBC driver could not be found... did you install the 2012 Native Client from here: http://www.microsoft.com/en-us/download/details.aspx?id=29065#SNAC ?
Yes i did :/. But did not restart PC if this is necessary.
DeleteHi Aleks,
DeleteCould you open your list of ODBC drivers and see if "SQL Server Native Client 11.0" is on the list?
A quick way to launch the ODBC Control Panel applet is Start / Run / then type "odbcad32" in the Run dialogue box. Select the "Drivers" tab when the dialogue is displayed.
Hi Rob,
Deletei see the SQL Server Native Client 10.0 on the list but not the 11.0.
Hey, it seems that this was the problem. I had an old version (they really shouldn't name the installation files allways the same). Thank you very much Rob !
DeleteAlso I did the same steps and I see my drivers are present.
DeleteSql server native client 11.0 and 10.0 are both on the obdc panel.
I get same the error as follow :(
Error information:
SQLSTATE: IMSSP
Code: -49
Message: This extension requires the Microsoft ODBC Driver 11 for SQL Server. Access the following URL to download the ODBC Driver 11 for SQL Server for x86: http://go.microsoft.com/fwlink/?LinkId=163712
SQLSTATE: IM002
Code: 0
Message: [Microsoft][Driver Manager ODBC] Nome origine dati non trovato e driver predefinito non specificato.
What can I do? Maybe the better solution is to migrate on linux and mysql
Thanks
This code works fine
Delete$pdo = new PDO("odbc:Driver={SQL Server};Server=$server;Database=$dbname;", $user, $pwd);
this isn't working
$connectionInfo = array( "Database"=>"$dbname", "UID"=>"$user", "PWD"=>"$pwd");
Does this work? :
Delete$pdo = new PDO("odbc:Driver={SQL Server Native Client 11.0};Server=$server;Database=$dbname;", $user, $pwd);
Yes it does
DeleteCode working
Delete$pdo = new PDO("odbc:Driver={SQL Server Native Client 11.0};Server=$server;Database=$dbname;", $user, $pwd);
Not working code :(
$connectionInfo = array( "Database"=>"$dbname", "UID"=>"$user", "PWD"=>"$pwd");
$conn = sqlsrv_connect($server, $connectionInfo);
if (!$conn)
die('Error
'. print_r( sqlsrv_errors(), true));
this code works also on another server.
I'm using the following extensions in php.ini
extension=php_sqlsrv_54_ts.dll
extension=php_pdo_sqlsrv_54_ts.dll
with PHP Version 5.4.38 with Thread Safety enabled
My server is a windows 2012 with apache and php
Any ideas?
Thanks
Where did you get the sqlsrv drivers from? Is everything 32 bit, Apache and php? Your error indicates the driver is loading successfully but not loading the odbc native client.
DeleteWindows server is 64 bit and apache with php are 32 bit
DeleteTo be honest I'm not sure what's wrong. You could try installing the x86 VC redistributables for VC9, and VC11, and if that doesn't work, try using my builds of the drivers in a blog labelled 'Unofficial php_sqlsrv...'
DeleteLinks to VC redists are here:
http://www.microsoft.com/en-gb/download/details.aspx?id=30679 (choose x86)
http://www.microsoft.com/en-gb/download/details.aspx?id=5582
great stuff but:
ReplyDeletewhen I try to install the X64 SNAC for 2008 R2, I get told that "the feature you are trying to use is on a network resource that is unavailable" and then "the installation source for this product is not available". is the sqlnclix64.msi file not a standalone installer for the native client?
thanks
John
ah. had moved original install files to savfe space.
ReplyDeleteonce I pointed to that folder, install was fine.
John
Thanks Rob! this solved my issues on xampp
ReplyDeleteI've been stuck on this all afternoon. Here's what I have:
ReplyDeleteWindows 2008 Server 64-bit
SQL 2012 Natice Client installed
PHP version 5.3.8
SQLSRV30 files extracted to correct ext folder
Zend Extension Build shows API220090626,NTS,VC9
The following lines are in the correct php.ini file:
[PHP_SQLSRV_53_NTS]
extension=php_sqlsrv_53_nts.dll
[PHP_PDO_SQLSRV_53_NTS]
extension=php_pdo_sqlsrv_53_nts.dll
I have entered IISRESET and rebooting the OS, but Registered PHP Streams does NOT include sqlsvr.
What am I missing???
Thanks,
Dan
Hi Dan
DeleteEverything looks ok. Have a look in your PHP log file to see if there are any error messages.
The PHP error log has not been updated (this is a VM made from an image of a production server. Is there anything other than the IP & Name that needs to be changed?)
DeleteAs I stated, this is a VM made from an image of the production server. The bindings had not been reset so my phpinfo page & SQL test query PHP page were running on the production server, where SQLSRV30 had not yet been placed.
DeleteNevermind...
Glad it's working Dan.
Deletethanks a lot man
ReplyDeleteThis comment has been removed by the author.
ReplyDeletei get stuck find solution installing php driver until i found your blog. and now my problem was end. thanks a lot, your article really helpfull.
ReplyDeleteThis comment has been removed by the author.
ReplyDeleteHi Rob,
ReplyDeleteI did the same steps you mentioned but i am not able to run sql server and continuously getting Fatal error: Call to undefined function mssql_connect() . Please can guide me about what can be the problem.
Windows 7 64 bit.
PHP 5.3.13
Sql Server Native client Installed and can see 11.0 in Drivers List.
For SQLSRV30.EXE, PHP 5.3, TS, VC9 add lines:
[PHP_SQLSRV]
extension=php_sqlsrv_53_nts.dll
[PHP_PDO_SQLSRV]
extension=php_pdo_sqlsrv_53_nts.dll
Help will be highly appreciated.
Thanks
This sqlsrv driver from Microsoft is not a straight upgrade of the old mssql driver (which was programmed by the php group).
DeleteInstead of mssql_connect you need to call sqlsrv_connect().
Some work is required in older php apps, usually an abstraction layer provides the best way to upgrade. You write each of the old mssql functions in php code, which map and translate calls to the new driver.
Hi Rob,
ReplyDeleteThanks for your reply i am trying to connect using sqlsrv_connect() but still getting the same error. but i have found some warning in Apache log.
PHP Warning: PHP Startup: Unable to load dynamic library 'E:/wamp/bin/php/php5.3.13/ext/php_sqlsrv_53_nts.dll' - %1 is not a valid Win32 application.\r\n in Unknown on line 0
Warning: PHP Startup: Unable to load dynamic library 'E:/wamp/bin/php/php5.3.13/ext/php_sqlsrv_53_nts.dll' - %1 is not a valid Win32 application.
in Unknown on line 0
PHP Warning: PHP Startup: Unable to load dynamic library 'E:/wamp/bin/php/php5.3.13/ext/php_pdo_sqlsrv_53_nts.dll' - %1 is not a valid Win32 application.\r\n in Unknown on line 0
Warning: PHP Startup: Unable to load dynamic library 'E:/wamp/bin/php/php5.3.13/ext/php_pdo_sqlsrv_53_nts.dll' - %1 is not a valid Win32 application.
in Unknown on line 0
and my OS is 64 bit... is that wamp which is 32 bit?
Please help me out.
Thanks
Yes you need the 32 bit version of PHP. If using WAMP, install the 32 bit version. See my next comment to Maximiliano too...
DeleteHi Rob, I already did that, installed everything in 32 bit and, no success either.
DeleteSo, I installed a wamp solution, WampServer, never used this things so, I don't know if this is a good product but, with that the driver is loading normal!
I still want make the things work manually, the WAMP solution have PHP 5.4.3 and PHP is already in 5.4.11 build, I want everything updated but, I'm tired of trying for now.
Many thanks to your reply, in a few days I'll probably try again, I'm a kind of crazy with things that I want to do.
Best regards
Hi man, I'm trying to make things work for mu on windows + php + sql server but, I just can't go to success.
ReplyDeleteI just did what your article says but, no success.
My configuration is:
Windows Server 2008 R2 64 bits (Amazon AWS)
Apache 2.4.3 64 bits
PHP 5.4.11 Non Thread Safe VC9 running with fast-cgi
I already tried everything that I know but, but no success.
My phpinfo file doesn't show me the extensions loaded and the function sqlsrv_connect return the undefined error.
So, if you can help me I'll appreciate, thanks anyway.
Is that a 32-bit build of PHP, downloaded from http://windows.php.net/download/ ?
DeleteAssuming it is, make sure that you have installed the VC++ 32-bit redistributables, which may not already be installed on Win64. Install both of these for good measure:
VC10 x86: http://www.microsoft.com/en-gb/download/details.aspx?id=8328
VC9 x86: http://www.microsoft.com/en-gb/download/details.aspx?id=5582
Have a look at your php log file to see if any messages there give you a clue. If not, you could also look in the Windows Event Viewer.
Just a quick note here - to use the SQLSRV driver you need 32-bit PHP, as standard.
ReplyDeleteIt should run on Apache 64 bit + mod_fcgid.
Install the x86 VC++ redistributables from Microsoft (see comment to Maximiliano above).
Then set up mod_fcgid... see: http://httpd.apache.org/mod_fcgid/mod/mod_fcgid.html
Here is how I set it up in httpd.conf Apache 2.2 on a test server (a production server should have different timeouts):
LoadModule fcgid_module modules/mod_fcgid.so
...
#Change any of these as required:
FcgidInitialEnv PHPRC "C:\Program Files\PHP\"
FcgidFixPathinfo 0
FcgidIOTimeout 1800
FcgidConnectTimeout 10
FcgidIdleTimeout 1800
FcgidBusyTimeout 1800
FcgidMaxRequestsPerProcess 500
FcgidProcessLifeTime 3600
FcgidMaxRequestLen 20000000
FcgidOutputBufferSize 0
OutputBufferSize 0
FcgidWin32PreventOrphans On
...
AddHandler fcgid-script .php
FcgidWrapper "C:/Progra~1/PHP/php-cgi.exe" .php
...
# don't forget to include directive ExecCGI to your directory Options.
# e.g. Options MultiViews Indexes FollowSymLinks ExecCGI
Hello Rob, I'm trying to install Native Client 2012 from the same link you posted, but when I try to run it the following error appears: 'There is a problem with this windows installer package, missing a dll ...'
ReplyDeleteI'm using: Windows 7 Pro x64, x86 apache 2.2, php 5.4 ts, sqlsrv.exe 3.0 (php_sqlsrv_54_ts).
Can you help me? My email andre@andrewd.com.br
It sounds like an issue between Windows 7 and Windows Installer. There are some solutions on Google - try doing a search using the exact error message you are seeing.
DeleteHello Rob, thank you very much for the great post! After getting your code for Windows XP and PHP 5.4, I did the following:
ReplyDelete1) extracting the files "php_sqlsrv_54_ts.dll" and "php_pdo_sqlsrv_54_ts.dll"
2) pasting the files into the php/ext directory
3) defining the values
extension=php_sqlsrv_54_ts.dll
extension=php_pdo_sqlsrv_54_ts.dll
in php.ini
After restarting the webserver, I got the following error:
"CDbConnection failed to open the DB connection: SQLSTATE[08001]: [Microsoft][SQL Server Native Client 10.0]Named Pipes Provider: Could not open a connection to SQL Server [53]."
I already did the TCP/IP changes on the server, but it still did not work. What could it also be?
Hi Maurice, this is a SQL Server security problem you have to go to the SQL Server Configuration tool, look for Named Pipes protocol and give access to the IP addresses you are using to connect into SQL Server.
DeleteHey Maximiliano, thanks for the fast reply! Now I have activated the protocol over the configuration manager and over "Cliconfg" but the problem still persists. Is there any documentation that describes the process in detail?
DeleteHi again Maurice, try this article, just check if the things are like this.
DeleteRemember that the IP address you are using in the connection parameter must be enabled in SQL Server
http://blog.sqlauthority.com/2009/05/21/sql-server-fix-error-provider-named-pipes-provider-error-40-could-not-open-a-connection-to-sql-server-microsoft-sql-server-error/
That was exactly the guide that I was trying out before :). Also I tried to temporarily deactivate the firewall, but had no luck :(.
DeletePlease, show me your connection config. Just the server part.
DeleteOkay, here it is:
ReplyDelete- checked the checkbox "Allow remote connections to this server"
- in "Cliconfg" the configuration on the server is:
- "Enabled protocols by order" -> TCP/IP | Named Pipes
- checked checkbox "Force protocol encryption"
I restarted the server after applying these changes. If you need some more info, please tell me.
I want the PHP config, the connection parameters in PHP script, you of course omit the user and password lines.
DeleteNo, the configuration worked before. But there seems to be another problem that I have to solve at first. The system seems not to be reachable over the network. I need to check this. Maybe this is already the answer. But thank you very much, you helped me a lot!!!
ReplyDeleteOk man, my guess is about the IP address that you are trying to bind.
DeleteI'll give you my SQL Server config that is working fine on a AWS Machine:
Client Protocols:
TCP/IP:
Default Port: 1433 (MUST BE SET);
Enabled: Yes
Keep Alive: 3000
Keep alive internal: 1000
Named Pipes:
Default Pipe: sql\query
Enabled: Yes
Wow amazing information about PHP development. PHP is best programming for custom website design in easy way so speedy also. PHP Application Development Company provide total solution for PHP.
ReplyDeleteWooo hoo! bro... this worked like a charm. I followed your step by step instructions and it worked at first shot.
ReplyDeleteMSSQL is talking to me through a blazing fast channel. This is why I love open source.
What MS stopped supporting, you blazed through it.
I can't thank you enough!!
Paras Parmar
This is great tips for microsoft sql server intallation.
ReplyDeletePHP Development Company
Hi, Thanks for great information
ReplyDeleteI have PHP 5.3.10 installed on 2008 64bit.
I downloaded the SQLSRV30.EXE as well as SQL native client 11. I even updated VC++ VC10 and VC++ VC10
I added these to php.ini
[PHP_SQLSRV]
extension=php_sqlsrv_53_nts.dll
[PHP_PDO_SQLSRV]
extension=php_pdo_sqlsrv_53_nts.dll
But after restarting IIS I dont see these driver in my phpinfo.
also I am running Plesk 11 and there are serveral php.ini (but based on plesk the main one is on
C:\Program Files (x86)\Parallels\Plesk\Additional\PleskPHP53
but I dont see the MS SQL driver at all
can You tell me what is am doing wrong?
Thanks again
Thanks a lot, this is so usefull.
ReplyDeleteIt works! Thank you.
ReplyDeleteYou might want to edit this part...
ReplyDeleteFor SQLSRV30.EXE, PHP 5.3, TS, VC9 add lines:
[PHP_SQLSRV]
extension=php_sqlsrv_53_nts.dll
[PHP_PDO_SQLSRV]
extension=php_pdo_sqlsrv_53_nts.dll
...to
For SQLSRV30.EXE, PHP 5.3, TS, VC9 add lines:
[PHP_SQLSRV]
extension=php_sqlsrv_53_ts.dll
[PHP_PDO_SQLSRV]
extension=php_pdo_sqlsrv_53_ts.dll
Thanks, good spot Tetsu.
DeleteReally useful, thanks Rob. There's alot of info out there for installing SQLSRV on PHP, but it's not collated anywhere as well as you have here. Cheers.
ReplyDeleteThanks for your work!
ReplyDeleteFor Sqlserver with PHP 5.4 on IIS6 the Yours is the only way!
Marco
robe Thanks, was very helpful tutorial
ReplyDeleteHello,
ReplyDeleteI have problem with my first test app using sqlsrv.
I downloaded php_sqlsrv_53_ts_vc6.dll and copied to php/ext, update php.ini.
So I can see sqlsrv in phpinfo, but when I try sqlsrv_connect it returns " This extension requires either the Microsoft SQL Server 2008 Native Client (SP1 or later) or the Microsoft SQL Server 2008 R2 Native Client ODBC Driver to communicate with SQL Server. Neither of those ODBC Drivers are currently installed. Access the following URL to download the Microsoft SQL Server 2008 R2 Native Client ODBC driver for x86: http://go.microsoft.com/fwlink/?LinkId=163712"
When I run odbcad32 I can see SQL Server Native Client 11.0
Is there anything else, to make it work?
I didnt create any database yet, and actually I dont know how (is there something like phpmyadmin for mysql?)
I am quite desperate, and running out of ideas, how to solve it :-(
Thanks for every help.
Otto
Try installing SQL Server 2008r2 Native Client:
Deletehttp://www.microsoft.com/en-us/download/details.aspx?id=16978#SNAC
On the Microsoft site, click 'Install Instructions' to drop down a list that contains SQL Server Native Client.
You can install it side-by-side with Native Client 11 (confusingly, that is the 2012 client)... afterwards you should see a new driver appear named 'SQL Server Native Client 10.0'.
Nice tutorial keep on writing
ReplyDeletejobs in burdwan
Thanks for all the comments.
ReplyDeleteI am currently working on builds for PHP 5.5.
Thanks Rob for this resource.
ReplyDeleteI have a WinXp SP2/ Apache 2.2 / PHP 5.4 working well but I would like to have the MSSQL extensions (both normal and PDO).
I've downloaded your PHP_SQLSRV30 (the files inside are named exactly to the official drivers, not as said in point 3.3 ???) and extracted the content of the 'bin' folder to my extensions folder.
I've installed the SQL Server Native Client 2008 x86 perfectly, I run the command: >"C:\PHP\php.exe" -i | more < and the PHP Extension Build is TS,VC9, I copied the appropiate Dynamic Extensions:
[PHP_SQLSRV]
extension=php_sqlsrv_54_ts.dll
[PHP_PDO_SQLSRV]
extension=php_pdo_sqlsrv_54_ts.dll
If I restart the Apache, i get an error. If I try the to get information from the php startup with >"C:\PHP\php.exe" -i | more <
I have an alert saying: error starting because MSVCP100.dll has not been found. After accepting the message, I get a PHP Startup: Unable to load dynamic library "c:/php\ext\php_sqlsrv_54_ts.dll" Module not found. But there is a file named like this in this folder????. And the same with the pdo dll.
I'm lost, any suggestions?
Done, and working.
ReplyDeleteIt was the MSVCP.dll, I've installed the Microsoft® C Runtime Library, Microsoft® Visual Studio® 2010 and restart the computer, OK.
Hope that helps.
Thanks Carlos, I've altered the orange text slightly.
DeleteYou must be using my unofficial build, there's a very small note underneath about VC10.
For everyone else, this is the link to the VC++ 10 redistributables: http://www.microsoft.com/en-gb/download/details.aspx?id=8328
Hi Rob,
ReplyDeletei am using windows xp with PHP 5.4 and i have followed your instructions and updated php.ini file as [PHP_SQLSRV]
extension=php_sqlsrv_54_ts.dll
[PHP_PDO_SQLSRV]
extension=php_pdo_sqlsrv_54_ts.dll
when i connected to sql server 2008 database, i am getting the below error.
[Microsoft][SQL Server Native Client 10.0][SQL Server]Login failed for user 'user'
but when i use the same user in sql server management studio i am able to login to the database.
please help me out.
Hi Rob,
ReplyDeletePlease find my details:
PHP Version => 5.4.15
System => Windows NT SABI-MGSWD01 6.1 build 7601 (Windows Server 2008 R2 Standard Edition Service Pack 1) i586
Compiler => MSVC9 (Visual C++ 2008)
Architecture => x86
PHP Extension Build => API20100525,NTS,VC9
Thread Safety => disabled
IIS 7.5 is the Web Server (IIS) role in Windows Server® 2008 R2
1. I have installed the driver(SQLSRV30.EXE) for PHP5.4.
2. Added following code in php.ini
[PHP_SQLSRV]
extension=php_sqlsrv_54_nts.dll
[PHP_PDO_SQLSRV]
extension=php_pdo_sqlsrv_54_nts.dll
3. Restarted the IIS
4. Restarted PC
But I could not see the section 'PHP_SQLSRV' in my php.ini file
What should I do?
This comment has been removed by the author.
ReplyDeleteHi Rob
ReplyDeletethanks for the information, u are awesome...dont why microsoft have to make everything so complicated...
ERROR ::
ReplyDeleteWindows Server 2003 SP2/IIS6 and PHP 5.3.x?
Microsoft SQL Server 2008 Native Client is already installed
Array
(
[0] => Array
(
[0] => IMSSP
[SQLSTATE] => IMSSP
[1] => -49
[code] => -49
[2] => This extension requires the Microsoft SQL Server 2012 Native Client. Access the following URL to download the Microsoft SQL Server 2012 Native Client ODBC driver for x86: http://go.microsoft.com/fwlink/?LinkId=163712
[message] => This extension requires the Microsoft SQL Server 2012 Native Client. Access the following URL to download the Microsoft SQL Server 2012 Native Client ODBC driver for x86: http://go.microsoft.com/fwlink/?LinkId=163712
)
[1] => Array
(
[0] => IM002
[SQLSTATE] => IM002
[1] => 0
[code] => 0
[2] => [Microsoft][ODBC Driver Manager] Data source name not found and no default driver specified
[message] => [Microsoft][ODBC Driver Manager] Data source name not found and no default driver specified
)
)
Thanks for tutorial Rob, Very well written !!
ReplyDeleteRob,
ReplyDeletethe app is throwing the following exception:
PDOException
SQLSTATE[28000]: [Microsoft][SQL Server Native Client 11.0][SQL Server]Login failed for user 'logon'.
oh.. my... GOD!!!!
ReplyDeleteHere's an alternative method for people who don't need MS for work:
1. Install debian/ubuntu/mint
2. sudo apt-get install lamp-server^
thanks a lot..
ReplyDeleteHi.. Rob,
ReplyDeleteI am using Wampserver2.4-x64(php 5.4.12 & apache 2.4.4) on windows sever 2012 (64bit) with sql server 2012 using SQLSRV30.exe for dll but unable to load the extensions even all are visible at wamp tray.
please help is these configuration are compatible.
The driver on this page only works with 32-bit php. It is possible to install 32-bit php on a 64-bit web server by using the fastCGI interface. PHP 5.5 comes with a 64-bit build, but there is no official MS release for the driver yet. A 64-bit php 5.5 sqlsrv driver can be found with a Google search, it was built by someone else.
DeleteHello Rob. I'm stuck on #5 -- the run command windows rushes through so fast I simply can not read anything at all. How do I get it to stop so I can scroll through it?
ReplyDeleteHi Sue, did you add ' | more ' to the end of your command? It should run the listing and prompt you to press a key when the page is full. You could always run this command instead:
Delete"C:\PHP\php.exe" -i > c:\phpinfo.txt
Then open file c:\phpinfo.txt to see the whole php information.
Thank you. I really appreciate the hand-holding. I am a complete newb. My PHP version is 5.5.6 and my Extension Build is NTS VC11. What driver should I be trying to use?
DeleteHi Sue, sorry but I've only got instructions up to PHP 5.4, I'm working on something else at the moment. The driver for PHP 5.5 hasn't been released by Microsoft yet, but someone has built one: http://sqlsrvphp.codeplex.com/discussions/441706
DeleteThe installation instructions follow the same pattern as the instructions here, try to use a common sense approach installing the PHP 5.5 VC11 NTS driver. You have not said if you're trying 32 or 64 bit PHP, that's an extra factor to consider, but choose the sqlsrv driver that matches.
Hi Rob
ReplyDeleteI have Os system like this :
Windows 8 64 bits
I installed : Apache 2.4.7 (Win64)
and php-5.5.7-Win32-VC11-x64
drivers Apache PHP : php5apache2_4
SQL server 2008 : Shard Memoy Enabled
Named Pipes Enabled
TCP/IP Enabled
VIA Enabled
I installed also The sql server 2012 native client
and the pilote SQLSRV30
I checked the odbc drivers there are :
SQL Server Native Client 10 and SQL Server Native Client 11, but when I go to SQL SERVER CONFIGURATION MANAGER, I see only the SQL NATIVE CLIENT 10.0
The issue that the page info().php is without-mssql" "--without-pdo-mssql ?
I realy need the help
Hello Wafa, you need to find a driver using Google as follows:
DeleteSqlsrv driver, php 5.5, thread safe, 64-bit.
This link might have it, it's nothing to do with me: http://social.technet.microsoft.com/Forums/en-US/e1d37219-88a3-46b2-a421-73bfa33fe433/unofficial-php-55-drivers-x86?forum=sqldriverforphp
Hi Rob, thank you for the post. It helped me a lot. Please keep on writing.
ReplyDeleteThis comment has been removed by the author.
ReplyDeleteYes, but the important compatibility is not the version of Windows, but these:
Delete* Your PHP version
* Your SQL Server Native Client version.
Thank you very much mate, you just saved my day!
ReplyDeleteFYI I had the same problem as Maurice, I solved by using in the DSN of the PHP PDO connection
the windows ODBC Data Source.
My PC config:
windows server 2003
MS SQL Server 2005
PHP 5.3.28
Basically I created a new Data source (you can create a new one trough the control panel/administrative tools/data sources) that point to the database and than I connected the PHP to the DB via PDO
new PDO("sqlsrv:Server=DATA_SOURCE_JUST_CREATED;Database=DATABASE_NAME", USER, PASS);
If you have to create a new data source please do not use spaces in the name, otherwise the hell will unleashed and you'll have to sell your soul :D
Thank you again!
Muchas gracias, fue una súper guía la mejor que pude encontrar.
ReplyDeleteEn mi caso el no funcionamiento del driver se debía a que Wampserver tiene que ser de 32bits.
Especificaciones por si alguien tiene las mismas características:
* Windows7 64Bits.
* Wampserver 2.2 32Bits.
* Driver SQLSRV30 64Bits.
* php_pdo_sqlsrv_53_ts
* php_sqlsrv_53_ts
Hello Rob, i so much love the way you respond to everybody. Please im having a problem and im at a client office and frustrated now .I have window sever 2008 64Bit, PHP 5.3 32bit installed using XAMPP. Apache 2.2.21 and MSVC9 (Visual C++ 2008) . In my php.ini file, pdo_sqlsrv is installed, pdo_odc is showing, all my dll are showing in the "ext" directory i also included pdo_sqlsrv_53_ts.dll and sqlsrv_53_ts.dll. ODBC Driver panel is also showing SQL Native Client 11 and 10. But my YII application is still showing error
ReplyDeleteCDbConnection failed to open the DB connection: SQLSTATE[IMSSP]: This extension requires the Microsoft SQL Server 2012 Native Client ODBC Driver to communicate with SQL Server. Access the following URL to download the Microsoft SQL Server 2012 Native Client ODBC driver for x86: http://go.microsoft.com/fwlink/?LinkId=163712
Please help meeeeee
Hi, I have WinXp SP3, Php 5.4.16, API220100525,NTS,VC9, MSSQL 2005. I found out your blog because I am having trouble to use CakePhp. The error is "CakePHP is NOT able to connect to the database."
ReplyDelete"Database connection "Sqlserver" is missing, or could not be created."
Shouldn't be an option like this "For SQLSRV20.EXE, PHP 5.4, NTS, VC9" in step 6?
I already have SQL Server Native Client.
I am running Apache 2.2 Zend.
Just to be sure I copy all dll files into phpext and try each option with NTS and VC9.
All of them gives me an error after restart Apache except this:
extension=php_sqlsrv_54_nts.dll
extension=php_pdo_sqlsrv_54_nts.dll
Could you please help me
Regards
Got it! Used: http://robsphp.blogspot.com/2012/06/unofficial-microsoft-sql-server-driver.html
DeleteThanks a lot!
This is great! thank you ever so much for this article. i have trying for hours on this until i figured out the problem thanks to this blog. the problem was SNAC was not installed!!!!!!
ReplyDeleteThis comment has been removed by the author.
ReplyDeleteAmazing, finally i reach the db engine, i got a error msj:
ReplyDeleteArray ( [0] => Array ( [0] => 28000 [SQLSTATE] => 28000 [1] => 18456 [code] => 18456 [2] => [Microsoft][SQL Server Native Client 11.0][SQL Server]Error de inicio de sesión del usuario 'NT AUTHORITY\SYSTEM'. [message] => [Microsoft][SQL Server Native Client 11.0][SQL Server]Error de inicio de sesión del usuario 'NT AUTHORITY\SYSTEM'. ) [1] => Array ( [0] => 42000 [SQLSTATE] => 42000 [1] => 4060 [code] => 4060 [2] => [Microsoft][SQL Server Native Client 11.0][SQL Server]No se puede abrir la base de datos "dbConsola" solicitada por el inicio de sesión. Error de inicio de sesión. [message] => [Microsoft][SQL Server Native Client 11.0][SQL Server]No se puede abrir la base de datos "dbConsola" solicitada por el inicio de sesión. Error de inicio de sesión. ) [2] => Array ( [0] => 28000 [SQLSTATE] => 28000 [1] => 18456 [code] => 18456 [2] => [Microsoft][SQL Server Native Client 11.0][SQL Server]Error de inicio de sesión del usuario 'NT AUTHORITY\SYSTEM'. [message] => [Microsoft][SQL Server Native Client 11.0][SQL Server]Error de inicio de sesión del usuario 'NT AUTHORITY\SYSTEM'. ) [3] => Array ( [0] => 42000 [SQLSTATE] => 42000 [1] => 4060 [code] => 4060 [2] => [Microsoft][SQL Server Native Client 11.0][SQL Server]No se puede abrir la base de datos "dbConsola" solicitada por el inicio de sesión. Error de inicio de sesión. [message] => [Microsoft][SQL Server Native Client 11.0][SQL Server]No se puede abrir la base de datos "dbConsola" solicitada por el inicio de sesión. Error de inicio de sesión. ) )
in few word i cant log in to the db engine, im working with a local instace that i call "." usng windows autentication, so i dont send user and pwd on my array...
what i can do to solve this?
tks for ur help
Hello David
DeleteThis proves the driver is installed and working. Your problem is authentication. Which user is attempting to gain access to your database? Grant access to the user and it will work.
From this message it looks like your web is running as 'LocalSystem', therefore so is PHP. Therefore, just add this Windows user ID to your logins and databases in SQL Server: NT Authority\SYSTEM, or LocalSystem.
hi Rob , i get error call to undefined function sqlsrv_connect (), with w7 sp1 ultimate mssql2012 and xampp php 5.5.9
ReplyDeleteI did all the steps, but in the end when I run my application, I have an error, call to undefined function sqlsrv_connect (), but also did the same in w8 mssql2012 64bit with xampp 5.5.9 and if it worked
When it says 'undefined function', it means the driver isn't loaded. See if you can spot an error in the log about the driver failing to load.
Deletealready checked everything, the administrator of sources contains ODBC SQL Server Native Client 11.0 driver, my php is 5.4, I'm using xampp and I put everything continues the same error occurring
ReplyDeleteArray ( [0] => Array ( [0] => IMSSP [SQLSTATE] => IMSSP [1] => -49 [code] => -49 [2] => This extension requires the Microsoft SQL Server 2012 Native Client. Access the following URL to download the Microsoft SQL Server 2012 Native Client ODBC driver for x86: http://go.microsoft.com/fwlink/?LinkId=163712 [message] => This extension requires the Microsoft SQL Server 2012 Native Client. Access the following URL to download the Microsoft SQL Server 2012 Native Client ODBC driver for x86: http://go.microsoft.com/fwlink/?LinkId=163712 ) [1] => Array ( [0] => IM002 [SQLSTATE] => IM002 [1] => 0 [code] => 0 [2] => [Microsoft][ODBC Driver Manager] Nome da fonte de dados n�o encontrado e nenhum driver padr�o especificado [message] => [Microsoft][ODBC Driver Manager] Nome da fonte de dados n�o encontrado e nenhum driver padr�o especificado ) )
Just for good measure, please could you look at step 3.1.1 or 3.1.2 and install the native client again, it will not harm your computer. Then restart apache before you retry.
DeleteIf it still does not work, please post your Windows version, and the php code you are trying to run.
This comment has been removed by the author.
Deletealready reinstalled the whole package of 11 Native Client, restarted apache and still the same error, my SQL Server is 2008, my Windows 7 Ultimate is the SP1, 32-bit, since I do not know what to do
Deletemy code
$ db, "UID" => $ user, 'PWD' => $ password);
$ conn = sqlsrv_connect ($ serverName, $ conninfo);
if ($ conn === false) {
echo "connection failed
.";
die (print_r (sqlsrv_errors (), true));
}
?>
Hi, thanks for the information. There is another comment on this page from someone who had a similar problem, please could you check to see what drivers are on your system:
Deletehttp://robsphp.blogspot.com/2012/09/how-to-install-microsofts-sql-server.html?showComment=1351463369681#c1879884186549071826
Hi Rob,
ReplyDeleteI was trying to connect php to sql server. I already did it in my local but I was experiencing error while doing it in the server. I already put the library in the ext folder and edited the php.ini however I'm getting this error message "Call to undefined function sqlsrv_connect()". Btw I'm using WAMP server
Hi, the driver isn't loaded... check you've installed the SQL Server native client, which is Microsoft's odbc driver (there are direct links to it in step 3 above). If you have - let me know what version of Windows and PHP you have, and whether your WAMP version is 64 bits, and I'll try to help.
DeleteYou might need a newer build of the driver, which is in a page on this blog called "Unofficial Microsoft SQL..."
This works on Windows Server 2012 R2 with SQL Server 2005?
ReplyDeleteBecause I follow the instructions, and works in Windows 8, but in the Server the driver isn't show in the phpinfo() from XAMPP.
Thanks in advice for the attention ;)
It's Windows Server 2008 x64, PHP 5.5 x86, SQL Server 2005.
DeleteAlso, I need to install the Apache service?
Hi Eduardo, for PHP 5.5 you need the unofficial driver, please download it from here:
Deletehttp://robsphp.blogspot.com/2012/06/unofficial-microsoft-sql-server-driver.html
The instructions in orange on this page will work for the unofficial driver (start from 1.3 orange text)
By the way, you don't need Apache, use IIS with Windows, it's an excellent web server.
I follow the orange instructions. In my laptop works great, the unofficial driver for PHP 5.5 in Windows 8 with Apache connect to SQL Server 2008
DeleteBut in the Windows Server 2008 x64 with PHP 5.5 x86 in Apache the sqlsrv isn't show in the phpinfo(). I'm not sure if is due the WS 2008 or an error installing Apache, or maybe I do something wrong in the instructions.
PS The server have install the SQL Server 2008 Native R2 Client, SQL Server Native Client and the databaase is SQL Server 2005
Many thanks for your answers :)
All I can see from your config is that you have Apache with 32-bit PHP on Win 64... you need one of these types of config:
Delete1. Apache 32-bit VC11 build (from ApacheLounge) using PHP module, and PHP thread-safe with TS sqlsrv drivers
2. Apache 64-bit and mod_fcgid (or other fcgi) and PHP non thread-safe, with NTS sqlsrv drivers to match
For both of these, you need the 64-bit SQL Server Native Client - you can check if it's installed by running ODBCAD32.EXE and looking at the 'drivers' tab. Hopefully you will see "SQL Server Native Client 11.0" on the list.
The server have the first configuration (PHP 5.5 32-bit, VC 11 and TS). Don't have installed the SQL Server Native Client 11, but my laptop neither have it installed and it runs.
DeleteAlso, in the XAMPP Control Panel, when start the Apache as a sevice, and having extension=php_sqlsrv_55_ts.dll
extension=php_pdo_sqlsrv_55_ts.dll
uncommented in the php.ini, the server shows "httpd.exe Failed to start because MSVCP110.dll was not found". But the file exists in C:\xampp\php. And when commented those lines, or when start the Apache uninstalling the service, the message is not display - but still not showing the sqlsrv in the phpinfo()
Where can be my error?
I'll have a look at XAMPP. There should be no reason at all to ship MSVCP110.DLL in the PHP folder, it is wrong. It is part of the Visual C++ redistributable, and Microsoft installs it within Windows. It should never be part of an application.
DeleteI will let you know what I find out about XAMPP later, but if they have put the VC11 redistributables in the PHP folder, they are using Windows incorrectly, it's a botch.
Step 1:
Did you try running ODBCAD32.EXE? If you didn't see the SQL Server Native Client in the list, please return to step 3.1.1 and install it.
Step 2:
Install the Microsoft redistributable for VC11 from here:
http://www.microsoft.com/en-gb/download/details.aspx?id=30679
and select "VSU_4\vcredist_x86.exe" when you click 'Download'.
FANTASTIC!!! It was, the Microsoft redistributable for VC11 fix the problem! Now the sqlsrv appears in the phpinfo().
DeleteNow it's time to create a connection for testing.
Thank you so much from Mexico, Rob :)
Glad it's working, thanks for persevering, your effort will help others if they have the same problem.
DeleteThank you!!! Microsoft redistributable for VC11 fixed the issue for me...
DeleteHi Rob,
ReplyDeleteI have followed your instruction then how do I know my php can connect to mssql ?
currently I use yii framework
I have still got this massenge
PDO MSSQL extension (pdo_mssql) Warning All DB-related classes Required for MSSQL database from MS Windows
PDO MSSQL extension (pdo_sqlsrv) Warning All DB-related classes Required for MSSQL database with the driver provided by Microsoft.
If you want to know if the driver's working, call function phpinfo() and look for a section called "pdo_sqlsrv"; if you see it, it worked.
DeleteSorry but I can't help with yii framework, or pdo_mssql, you have to ask them about it.
Hi Rob,
ReplyDeleteI'm running WAMP on my local computer for a test environment. I have the 32-bit version intalled with PHP 5.4. I'm now trying to connect to an officer service running a sql server instance (IIS7) and get the following error:
Fatal error: Uncaught exception 'PDOException' with message 'SQLSTATE[IMSSP]: This extension requires the Microsoft SQL Server 2012 Native Client ODBC Driver to communicate with SQL Server.
I've already installed the native client on my local computer and verified it is runnning with odbcac32. Is there a way to get this setup to work? Does native client need to be installed on the server as well?
Thank you!
It's difficult to work out exactly what you mean, but if your officer service runs php with sqlsrv under IIS7 then that computer needs the native client.
DeleteOk I'll try to clarify:
DeleteOur company has a server set up running IIS with a 2008 SQL instance. As far as I know, PHP is not installed on this server. I can connect to this instance and the DBs from my local computer using SQL management studio (windows authentication). I usually use the databases to pull data for reports etc. and also have a SSRS running.
I have WAMP installed on my local with PHP 5.4 (32-bit) to test an interface in building. sqlsrv and native client 11 are installed on my local computer and enabled in WAMP. I would like the interface to connect to the SQL instance on the company server for testing but not sure if that's possible?
As mentioned before, the error I get is related to the native client, which probably means it's not installed on the company server. Does that make more sense now?
I guess I could set up a local instance of sql server and find out pretty quickly if that works.
thanks
I assume then that you're connecting to SQL Server 2008 on your company's server. (It doesn't matter if it's running IIS.)
DeleteAnd you are using your local computer with PHP to connect to SQL Server, so your configuration should be ok.
But you get a message that says the Native Client is not installed - that message is usually right, so it's likely that you have the wrong version of the Native Client installed (e.g. maybe SQL Server Native Client 10.0).
For good measure, please run step 3.1.1 (if your local computer is 32-bit Windows) or 3.1.2 (if it's 64 bit Windows) on this page.
Thank you Rob!!!
ReplyDeleteI went to step 3.1.2 and it resolved the issue. Obviously I must have had the wrong version of the native client. I set up a test account on the sql server and can now access the DB through PHP.
Thanks again for your help.
Btw, do you know what's going on with PHP 5.5/5.6 support? Seems like MS is behind the curve.
You can get my builds for PHP 5.5 and 5.6 on this blog page titled "Unofficial php_sqlsrv...". They are safe I use them myself and support them. There are also builds on ApacheLounge by Jan Ehrhardt.
DeleteRegarding Microsoft's position, I don't think they are active any more. The last comment in July was made here:
https://connect.microsoft.com/SQLServer/feedback/details/790641/sql-server-driver-for-php-5-5#details
I read it as saying they have given the project to the PHP group to run with. It would be ideal if each Windows version of PHP (maybe all platforms) was shipped with the driver among the bundled extensions.
It's a good driver and better than anything else available on PHP for ODBC or SQL Server, but it could be even better. There is unnecessary clutter and code churn, and areas where it could be optimised significantly.
I've been a Microsoft developer for a long time and feel I know them quite well... this PHP driver was something that surprised me (pleasantly), but in the back of my mind I was waiting for them to drop it. It's not their thing, but in order to extend the reach of SQL Server and IIS, they needed to embrace PHP and other open platforms. To be honest IIS, SQL Server and PHP is a formidable combination, so I hope it picks up again.
In the mean time, we have the source code and can build or change it according to the Apache licence they distributed it under.
Thanks for the info. It sounds like giving the project to the PHP group will turn out to be a good thing in the long run. It makes sense to include the driver with each new release. In the meantime I'll check out your builds.
Deletecheers
For days I have searched the Internets for a solution to make my Apache with PHP 5.5 able to talk to MSSQL Server.
ReplyDeleteI found many solutions, and none of them worked. Then I found this page, followed the (very clear) instructions to the letter and voila: It works!
Thank you very, very much!
Thank you very, very much!
ReplyDeleteThis is really great efforts of this blog owner to take a time for explaining the things about install the microsofts sql in PHP server.I really appreciate.
ReplyDeleteWebsite Design Companies Bangalore | Website Design Company Bangalore
Rob,
ReplyDeleteI am attempting to move a PHP site developed in PHP 5.2.13 to 5.3.13. have followed your article very closely, however it appears I am still unable to connect to a SQL database. I added the extensions to the PHP.INI file. From the site logs, it doesn't appear to be finding the driver? Is there something in the "ADODB" library that I need to change to point to the new SQL driver or other code changes?
IIS 7
Windows Server 2008 X64
PHP 5.3 NTS
SQL V11 native client
Various Errors in the logs
PHP Notice: Use of undefined constant query - assumed 'query' in E:\WebSites\XXX
PHP Notice: Use of undefined constant ABC - assumed 'ABC' in E:\WebSites\XXX
PHP Notice: Undefined index: query in E:\WebSites\XXX LINE # XX
PHP Notice: Undefined index: filter in E:\WebSites\XXX LINE # XX
Those errors are not related. In new php, you must quote array indices, e.g $var[query] becomes $var['query']. Php is warning you that it has assumed the quotes are missing.
ReplyDeleteI think your problem here is because you haven't got the right version of the SQL Server Native Client installed. Please install version 10 (windows 2008r2). There is a link in the instructions.
Rob,
ReplyDeleteThanks for the quick response. I was only looking at this issue one sided (logs only). I was actually receiving a connection failed message on the site. I moved forward with install of SNC 10.0 anyway and i still receive the same error "Connection Failed".
I am not a developer, but I am attempting to move the code from Win2k3 to Win2k8. Is there a basic SQL PHP script I can test the SQL connections string to ensure it is using the correct driver and eliminate any code errors? I tried the code here for the connection string and put a bad password in the string, but it doesn't return anything. http://php.net/manual/en/function.mssql-connect.php
Hi Jordan, sorry but i'm not sure. You could have a look in Microsoft's forum for the php driver, or paste the error from your php log file here.
ReplyDeleteNo there aren't any specific test scripts, but Microsoft's forum contains many code examples, you might be able to create a test script.
Hi Rob,
ReplyDeleteGreat Blog - thank you.
After getting the error "CDbConnection failed to open the DB connection" I turned to your blog and got it all working on a 32-bit Win7 host with 32-bit PHP and a remote SQL Server 2012 - thanks.
But, now the time has come to get this onto a server and I'm getting the same error back.
Basically, I have performed installs on 2 Win2008R2 (64-bit) servers using the same 32-bit PHP, same Microsoft drivers and the 64-bit version of the SQLSRV30. This time I am trying to connect to MS SQL Server 2012 Express on the same host.
Every time I try I get the "CDbConnection failed to open the DB connection" error and cant find out why?
Would you be able to offer any ideas please? Many thanks
Andy.
Hi Andy,
DeleteSQLSRV30 is the PHP driver - you need the 32-bit version because it has to match your PHP build.
You need the 64-bit version of SQLNCLI (SQL Server Native Client 2012).
I'm guessing that's what you meant.
Can you have a look in your PHP log file to see if you see any errors from the sqlsrv driver, the CDbConnection error doesn't help much.
I have found on a few occasions I needed to manually install Microsoft's Visual C++ 2012 Redistributable package 32-bit version, it might be that the driver build needs an updated version.
A good way to test if you need it is run php.exe -i -c "your php.ini path" from the command line - any extension errors will pop up in a message box.
Install the Microsoft redistributable for VC11 from here:
http://www.microsoft.com/en-gb/download/details.aspx?id=30679
Select "VSU_4\vcredist_x86.exe" when you click 'Download'.
Thanks for such a quick reply Rob, I've added the VC11 redist package for x86, and ran php.exe as you mentioned above with the parameters, there's no issues popping up.
ReplyDeleteI'm using the Bitnami Dreamworks install (for REST) and not too sure where PHP error log may be. I've checked the apache2/logs folder, but there's no errors in there relating to PHP.
Can you tell me what the error log file is usually called or where it may be please and I'll check that next as it still does not work.
Thanks again
Andy.
If you can find your php.ini file, it contains a setting called 'error_log' which will tell you where the log file is.
DeleteOr, does your package give you a phpinfo page? If not you can create a simple php script file, put it in your web site's folder and run it from your web page... e.g. create "phpinfo.php" containing this text, then browse to it:
phpinfo will also display the location of your error log.
My script example got wiped from that last message. Trying again:
Delete<?php
phpinfo();
?>
Wow, that tip about editing the .ini file to sort out the logging has helped me solve the issue.
DeleteAbsoultely nothing to do with the drivers, or the PHP settings, or anything PHP related.
Simply the database was set for Windows Authentication only, instead of SQL.....
Thank you ever so much for your words of wisdom and time. I can now move forward :-)
This comment has been removed by the author.
ReplyDeleteHi Rob,
ReplyDeleteI am getting error. I did all steps which you mentioned above. any help appreciated.
Array ( [0] => Array ( [0] => 08001 [SQLSTATE] => 08001 [1] => 53 [code] => 53 [2] => [Microsoft][SQL Server Native Client 11.0]Named Pipes Provider: Could not open a connection to SQL Server [53]. [message] => [Microsoft][SQL Server Native Client 11.0]Named Pipes Provider: Could not open a connection to SQL Server [53]. ) [1] => Array ( [0] => HYT00 [SQLSTATE] => HYT00 [1] => 0 [code] => 0 [2] => [Microsoft][SQL Server Native Client 11.0]Login timeout expired [message] => [Microsoft][SQL Server Native Client 11.0]Login timeout expired ) [2] => Array ( [0] => 08001 [SQLSTATE] => 08001 [1] => 53 [code] => 53 [2] => [Microsoft][SQL Server Native Client 11.0]A network-related or instance-specific error has occurred while establishing a connection to SQL Server. Server is not found or not accessible. Check if instance name is correct and if SQL Server is configured to allow remote connections. For more information see SQL Server Books Online. [message] => [Microsoft][SQL Server Native Client 11.0]A network-related or instance-specific error has occurred while establishing a connection to SQL Server. Server is not found or not accessible. Check if instance name is correct and if SQL Server is configured to allow remote connections. For more information see SQL Server Books Online. ) )
Thank you very much.I was fed-up with the issues.Now its solved with the help of your article.
ReplyDeleteShould use "sqlsrv_connect" instead of mssql_connect.mssql_connect showing error with the same setup.
ReplyDelete"Fatal error: Call to undefined function mssql_connect() ." If you have any sample code with mssql_connect ,please comment me.
Sample code i am using :
Delete$uid,
"PWD"=>$pwd,
"Database"=>$databaseName);
/* Connect using SQL Server Authentication. */
$conn = sqlsrv_connect( $serverName, $connectionInfo);
$tsql = "SELECT * FROM tblContact";
/* Execute the query. */
$stmt = sqlsrv_query( $conn, $tsql);
if ( $stmt )
{
echo "Statement executed.
\n";
}
else
{
echo "Error in statement execution.\n";
die( print_r( sqlsrv_errors(), true));
}
/* Iterate through the result set printing a row of data upon each iteration.*/
?>
Hi Rob,
ReplyDeleteI use an output storedprocedure and execute this from php with sqlsrv. When i used within the sp an execute command, the output var is empty. Without the execute command, the output parameter return the correct value.
Is this a known problem?
Hi Dick, yes it's a known issue. When you execute a stored proc, output parameters are only populated at the very end of processing - that means you have to call sqlsrv_next_result() to run through every result in your stored proc until it returns NULL. When sqlsrv_next_result returns NULL, your stored proc has completed and your output variables will be populated.
DeleteAlso note that if sqlsrv_next_result returns false, stop processing, an error has occurred and NULL will never be returned.
Hi Rob,
DeleteThanks for your clear answer.
This comment has been removed by the author.
ReplyDeleteThis comment has been removed by the author.
DeleteHi Rob,
ReplyDeleteI've got PHP 5.4, TS, VC9, windows7 64bits, SQL server 2012
I've installed SQL Server 2012 Native Client for 64, SQLSRV30.EXE and added the lines below to php.ini
;For SQLSRV30.EXE (or the custom drivers), PHP 5.4, TS, VC9 add lines:
[PHP_SQLSRV]
extension=php_sqlsrv_54_ts.dll
[PHP_PDO_SQLSRV]
extension=php_pdo_sqlsrv_54_ts.dll
but still cannot see sqlsrv on phpinfo page and gets the error Fatal error: Call to undefined function sqlsrv_connect()
I appreciate any help.
Guner
Hi Guner - what web server are you using? You've done all the right things and the php.ini lines you added are correct - your configuration is typically used with web servers like Apache. Your php error log will probably contain more details - stop and start the Apache service and see what the php error log contains after it starts up.
DeleteHi Rob! Thanks SO much - saved my day on Windows2003 with php 5.4 ts. Perhaps just mention that both drivers must be enabled in php.ini (not only the _pdo_ one)...
ReplyDeleteIt should work on its own, you might be referencing a constant from the sqlsrv extension. If you disable sqlsrv, your php error log might identify why it is required when you only need pdo.
DeleteHi Rob,
ReplyDeleteI have a problem installing SQLSRV in Microsoft Server 2012 R2 64bit.
My php version is 5.3.5 - 32bit, and the SQL Driver is 3.90
Here is the error that i encountered.
"WARNING: [Microsoft][ODBC Driver Manager] Data source name not found and no default driver specified"
Any Help would be great
Thank you in Advanced
Oh Sorry my bad the SQL Driver is 3.0.. :D
DeleteHi Carl, it could be a number of things...
DeleteHave you installed the native client in step 3.1.1 (in the instructions above)? If not, this should be all you need.
If it is installed, you could try installing this c++ redistributable:
http://www.microsoft.com/en-gb/download/details.aspx?id=30679
Sorry, I pasted the wrong link, it is this for PHP 5.3:
Deletehttp://www.microsoft.com/en-gb/download/details.aspx?id=5582
Hi Rob,
ReplyDeleteIm using Win Server 2008 +Apache 2.4.4(win64) +SQL Server Native Clients(2008 R2) and connect to SQL Server 2008 R2, when i treid add sqlsrv dll into php ext it show me some error like
PHP Warning: PHP Startup: Unable to load dynamic library 'ext\php_pdo_sqlsrv_55_ts.dll' - %1is not a valid win32 application
there are some error in different version, did i lost some thing ?
php-5.6.6-Win32-VC11-x64 + php_sqlsrv_56_ts.dll
php-5.5.22-Win32-VC11-x64 + php_sqlsrv_55_ts.dll
You only need the sqlsrv driver version that matches your php version - it should be the x64 version of php_sqlsrv_56_ts.dll. Remove the php 55 dll from your php.ini file.
DeleteThanks for such a quick reply Rob,
DeleteI've tried this combination this moning,this is setup in my php.ini.
[PHP_SQLSRV]
extension=php_sqlsrv_56_ts.dll ====>> download SQLSRV32.EXE from http://www.microsoft.com/en-us/download/details.aspx?id=20098
still not work .... :-(
Hi Rob,
DeleteAfter read your explanation a again...
Currently it use apache 2.4.12 x86 + php-5.6.6 x86, and php_sqlsrv_56_ts.dll-x86 download from you provided, it's work now !!
Thanks a lot,
I'm on WAMP 2.5/32bits/Windows 7 x64. What if I have the Native Client (v. 11), the redistributable (VC++ x86 2010), downloaded php_sqlsrv_3.0.2.2, enabled php_pdo_sqlsrv_55_ts.dll in my php.ini and it still doesn't come up when I do phpinfo()? No errors in logs either. At a loss ATM
ReplyDeleteThis is the vc redistributable you need:
Deletehttp://www.microsoft.com/en-gb/download/details.aspx?id=30679
Choose the x86 version.
If it still doesn't work after installing this and restarting Apache, have at look at your php error log.
Note that your Apache build should also be vc11.
Hi Rob,
ReplyDeleteI have been trying to connect to SQL Server 2012 (which is on a remote server) through php which is installed on my machine. I am using php v5.6.3. I also installed php_sqlsrv_56_ts.dll and php_pdo_sqlsrv_56_ts.dll, copied it in the php\ext folder and included it in the php.ini file. While restarting the apache server, I am getting an error stating that "PHP Startup : Unable to load dynamic library 'D:\PHP\XAMPP\php\ext\php_sqlsrv_56_ts.dll' - %1 is not a valid Win32 application". (My Xampp directory is D:\PHP\XAMPP) Is there any solution to this? I have tried searching for it on the internet but got no solution.
Thanks. :)
Hi Soumya, it could be any of these, or a combination,,,
Delete1. See if you have 32 or 64 bit XAMPP, and make sure the driver version matches the PHP version, details are in the instructions on this page.
2. You might be missing a Microsoft VC redistributable package. To be on the safe side install the latest ones for VC11 (2012). Make sure you choose 32 or 64 bit to match your PHP version:
http://www.microsoft.com/en-gb/download/details.aspx?id=30679
Awesome instructions! Up and running in no time flat!!!
ReplyDeleteThis is really fantastic documentation and explanation. I followed everything to a T. I have php 5.6, with 86x architecture. I installed the "unofficial" drivers into the ext folder, I have the 64 bit sql native client successfully installed. I restarted web services. I have been around the bend so much, and I was psyched to see your detailed instructions. Would you have any advice for a newb who is still struggling to get this darn thing configured? I'm banging my head, and it hurts lol.
ReplyDeleteoh, and I edited the php.ini to add the correct extensions for PHP 5.6, TS, VC11. which is what I have based on the command line we needed to run.
Deletemore info. When I run php.exe I get this:
DeletePHP Warning: PHP Startup: wincache: Unable to initialize module
Module compiled with build ID=API20131226,NTS,VC11
PHP compiled with build ID=API20131226,TS,VC11
These options need to match
in Unknown on line 0
PHP_MINIT_FUNCTION for php_sqlsrv: entering
PHP_RINIT for php_sqlsrv: entering
sqlsrv.WarningsReturnAsErrors = On
sqlsrv.LogSeverity = -1
sqlsrv.LogSubsystems = -1
sqlsrv.ClientBufferMaxKBSize = 10240
Hi Courtney, is it the sqlsrv driver you are trying to install? Your error says you have copied the wrong version of php_wincache.dll to your 'ext' folder...
DeleteBest way to fix is remove all extensions you think are causing problems in php.ini, then test. If it works, add them back one at a time and test until you find the one that isn't working.
DeleteThanks so much for responding, Rob. These are all the extensions I have enabled in the ini:
Deleteextension=php_wincache.dll
extension=php_pgsql.dll
extension=php_pdo_odbc.dll
extension=php_sqlsrv_56_ts.dll
extension=php_pdo_sqlsrv_56_ts.dll
When I comment them all out, and run php.exe I get nothing at all in the console. Is that right? When I just uncomment the sql ones and not the cache, I get the following:
PHP_MINIT_FUNCTION for php_sqlsrv: entering
PHP_RINIT for php_sqlsrv: entering
sqlsrv.WarningsReturnAsErrors = On
sqlsrv.LogSeverity = -1
sqlsrv.LogSubsystems = -1
sqlsrv.ClientBufferMaxKBSize = 10240
but still no sqlsrv section to the phpinfo(). Even after iis restarts.
Ok, you can permanently drop php_wincache, it only works with php nts version.
DeleteWhen you ran php.exe at the console it was probably just waiting for your commands, try typing something like: then ctrl-z and enter.
As far as iis goes, I recommend just stick to the console for now, then move onto iis once it's working. Typical iis requires php nts version with fastcgi.
Sorry, blogger deleted my php tags. Try typing something like: <?php echo 'Hello world'; ?> then ctrl-z and enter.
DeleteSorry, blogger deleted my php tags. Try typing something like: <?php echo 'Hello world'; ?> then ctrl-z and enter.
DeleteThanks. I'm not sure about a couple of things though. I typed that php test in the php.exe but it didn't echo the result. When I hit ctrl+z I just get ^Z in the cmd window.
DeleteMy bigger question (sorry to monopolize your wonderful knowledge) is about you comments on iis and nts. What do you mean by sticking to the console, and then moving on to iis once it's working? I ask because this is me learning, so that I can get to the level of doing. It's on me to configure this environment the best way possible, so I can build pages that can eventually be used for real stuff. For the nts issue, should I in a sense, start over?
Should I uninstall the php I have and reinstall the nts version or something? I might be missing something, but I may not really understand the difference or reason that there is ts and nts. It sounds like why would anyone want something that is not "thread safe", but maybe I don't get what that really means to the processes that run through php.
Thanks so much for all your comments and advice!
Nts is faster. In a multithreaded program it takes time to switch from one thread to another. Iis with fastcgi runs each php request in its own thread, so php never has to worry about locking data or switching threads. If you want true low latency the fewer context switches the better. So I think you should install php again and have a look on iis.net for info on php with fastcgi. Iis should load php-cgi.exe (via fastcgi). And look for the new opcache from php 55 onwards instead of using wincache.
DeleteThanks so much. A lot of this is foreign to me, so I definitely need to learn what all this means. It looks like I need to uninstall the php that I have and find the right version that I should install. Any tips on how to properly uninstall php, so I don't mess up the right install? lol It's so hard to have to go back to square one, but I want to do it right!
DeleteYes, I know exactly what you mean, we've all started somewhere - after a while it all makes sense, but I asked exactly the same questions at first. Would you like to email me at rob.johnson.a@gmail.com? We're going off topic a little bit, but I'll be happy to help.
DeleteThanks! I will email you!
DeleteRob, I have 2 php installed with iis: 5.2 and 5.5.
ReplyDeleteWhen i show the sql server data with php 5.2, the accents are appearing correctly.
But, when i show the sql server data with php 5.5, the accents do not appear correctly.
Something like: Ã ©, etc ...
Can you help me?
It looks like you might have sqlsrv set to utf-8 mode, but maybe your database code page is Latin? Are you using pdo or sqlsrv, and do you know your database locale? You might have to adjust the settings for sqlsrv in php 5.5.
DeleteI'm using PDO.
DeleteMy database is on my local machine.
When in use utf8_decode in ECHO the results with accents are displayed correctly.
That's a good practice? If not, what should I change the setting in php.ini?
Ok, that makes sense. PDO in version 5.5 (i.e. pdo_sqlsrv 3.x) defaults to UTF-8. If you want compatibility between both your PHP version, see pdo attribute PDO::SQLSRV_ENCODING_SYSTEM.
DeleteInstead of using utf8_decode, why not change the encoding of the results in your web browser to UTF-8 and see what happens?
So did the test as you said.
DeleteCalled header("Content-Type: text / html; charset = UTF-8", true) and the same thing happens.
I do not know where declare the PDO :: SQLSRV_ENCODING_SYSTEM attribute.
I tried to call this constant in the dsn, but to no avail!
=(
Hi,
DeleteI meant that you should tell your web browser to display the page in a different encoding, for example, in Firefox, open your page and select Alt-V then C to display or change the encoding.
I'm going to have to direct you to the sqlsrv forum, there are many questions and answers based on encoding issues:
https://social.msdn.microsoft.com/Forums/sqlserver/en-US/home?forum=sqldriverforphp
You sir, are a legend.
ReplyDeleteThanks a lot for this. Got it to work!
ReplyDeleteI keep getting "The system could not load this extension" when I restart PHP - but no other details in the log.
ReplyDeleteI am using PHP 5.2.9 on a Windows 7 PC.
The instructions say to use the SQLSRV30 download for Windows 7 ("Windows Vista or above") but there is not corresponding PHP 5.2 files.
Will PHP 5.2 not work with Windows 7?
The PHP 5.2 driver is only included with SQLSRV20.EXE. Follow the instructions for Windows XP.
DeleteThanks for the response...
DeleteI still get the same message though even with the php_sqlsrv_52_nts_vc6.dll file.
It's hard to tell, but you might need to install vc6 redistributable (32 bit), and make sure sql native client for 2008r2 is installed (matching Windows' bitness). Php 5.2 is old software now!
DeleteYes I've been assigned to a new business unit - not sure why we are using such an old version.
ReplyDeleteI have the VC Redistibutables from 2005 on up installed - any idea where I can find a VC6 installer?
Hi Kurt. I apologise for the bad advice I gave previously. VC6 is not supported any more by Microsoft, and many of the downloads for it on the web are viruses, so I don't recommend looking for it. If PHP 5.2 is working, then you should have the correct runtime files installed on your PC. You could try looking in the Windows event log for errors. Could you let me know what type of web server you are using, Apache, IIS, etc.
DeleteI am using IIS locally the SQL server is accessed through a VPN tunnel. It looks like others who have this set up here are not using any external extensions, just the php_mssql.dll. I have reinstalled this so many times that I think I am better off than I was before. My error when trying to run my app is now "[Microsoft][ODBC Driver Manager] Data source name not found and no default driver specified" - but we are not using ODBC just sqlsrv_connect.
ReplyDeleteOk, that's good! You just need to install the native client for SQL Server 2008r2. Pick the version that matches Windows, i.e. for Win 7 64 bit - choose the 64 bit version; for Win 7 32-bit, choose the 32 bit version.
DeleteNote that this driver includes the sqlsrv_connect function, it does not include mssql_connect.
Unfortunately I tried that, I uninstalled/reinstalled and I still get the same message. I also then tried installing "Microsoft® ODBC Driver 11 for SQL Server® - Windows" but it didn't help either.
ReplyDeleteHi, Rob.
ReplyDeleteI wonder if I have to install everything of SQL Native Client or just the first one?
If it is just the first one (sqlncli.exe), I followed everything you guided.
My info: Win7 Ultimate x64, WAMP 2.5, PHP 5.6.16 (upgraded from 5.5.12), TS, VC11
Thanks,
From frustrated man.
Do you have a build for PHP 7.X 64 bit?
ReplyDelete