如何用php连接sql server,如何用PHP连接SQL Server

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. Instructions:

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

If you have 64-bit PHP, please extract only the contents of folder x64of the zip file.

If you have 32-bit PHP, extract the files in the root of the zip and ignore any folders.

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

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.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值