PHP natively supports mysql, but sometimes I also want it to access the sql server, what should I do?

I recently found some information.Tested several cases of php accessing sqlsvr successfully,Time limited,Haven't tested more different environments,The test records are as follows:

Test environment:win7 x64 sp1, iis 7.5, apache 2.4 32-bit version, php 5.2.6 win32, php 5.6.11 win32 ts (thread-safe version)

Note:Because the "microsoft drivers for php for sql server" driver is currently only available in 32-bit version, if you are friends who use the sql server database,It is not recommended to use PHP 64-bit, otherwise it will cause problems connecting to the sql server database!

php 5.2.6 successfully accessed sql2000 on iis

php 5.2.6 successfully accessed sql2008r2 on iis

php 5.6.11 successfully accessed sql2008r2 on apache


Why use such an old version of PHP for testing?Because php 5.2 version, built-in php_mssql.dll module,

Opened the extension=php_mssql.dll configuration option in php.ini,Seems to support mssql2000 by default,

Open phpinfo to see the relevant support module content:

Installing php under ps.iis 7.0 is now easy,You can install it through a third-party gadget php manager, or you can easily switch the php version under IIS. This is not the main content of this article.Not introduced here;

php manager for iis 7 download and installation address:/

Write a small test routine:

$server=" \ sql2000";//Server IP address, if it is local,Can be written as localhost
$database="master";//database name
//Make database connection
$conn=mssql_connect ($server, $uid, $pwd) or die ("connect failed");
mssql_select_db ($database, $conn);
//Execute the query
$query="select * from cs";
$row=mssql_query ($query);
//Print the query results
while ($list=mssql_fetch_array ($row))
print_r ($list);
echo "<br>";

The results under iis 7.5 are as follows:Success!


PHP 5.6 and above no longer have php_mssql.dll, and it seems that it no longer natively supports sql2000 or later sql server. To use third-party modulesFound on Microsoft's official website:

microsoft drivers for php for sql server

The download link has four files:





Support different php versions respectively

•version 4.0 supports php 7.0+

•version 3.2 supports php 5.6, 5.5, and 5.4

•version 3.1 supports php 5.5 and 5.4

•version 3.0 supports php 5.4.

If it is php 5.2 to download a sqlsrv20.exe, this file can be easily found online now;

(Although the older version 5.2 should have been eliminated,But considering that some environments need to use it)



1. I have php 5.6.11 win32 ts in my hand, I want to use this



These two files,Copy to the php \ ext directory,

2. Add the following two sentences to php.ini:

extension=c:\ php5 \ ext \ php_sqlsrv_56_ts.dll

extension=c:\ php5 \ ext \ php_pdo_sqlsrv_56_ts.dll

3. Restart iis or apache,

4. You can see the following module display through phpinfo,Our php should be able to access sql server 2008r2 at this time;

5. Write a small program to verify:

try {
$dbname="sqlsrv:server= \ sql2008r2;database=master";
$db=new pdo ($dbname, $dbuser, $dbpassword);
if ($db)
echo "database connect succeed.<br />";
catch (pdoexception $e)
$content=iconv ("utf-8", "gbk", $e->getmessage ());
echo $content. "<br />";
//echo "hello pdo to ms sqlsrv!";

If the following prompt appears:

Maybe you also need to install microsoft&odbc driver 11 for sql server®-windows

If correct,It should be ok:

ps. Since the new module is accessing sql server 2008r2 through pdo, if it is accessed locally,Then you can access it directly through the ip address and sa account.

If php and sql2008r2 are on different computers,You must also set TCP port 1433 to access, see the second picture below,Change the dynamic port to 0 and set the port to 14333. If your sql2000 has occupied 1433, add one after the access addressxxxx port number

sqlsrv:server=192.168.1.xxx \ sql2008r2,14333;database=master

  • Previous AngularJS basic ng-cut instruction introduction and simple examples
  • Next Java selenium tutorial selenium detailed introduction