PHP is born to support MySQL, but sometimes you want it to access SQL Server, what to do?
Recently found some information, the test successfully PHP access to sqlsvr of several cases, limited to time, has not 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 Edition)
Note: Because the Microsoft Drivers for SQL Server driver is currently only 32-bit version, if you are a friend using a SQL Server database, it is not recommended that you use PHP 64 bits, or you will not be able to connect to the SQL Server database Problem!
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
"5.2.6-> SQL2000"
Why do you want to test this old PHP version of 5.2.6, because the PHP 5.2 version, built-in Php_mssql.dll module,
The extension=php_mssql.dll configuration option in PHP.ini is turned on, and the default seems to support mssql2000.
Open Phpinfo to see the relevant support module content:
Ps. IIS 7.0 install PHP is now convenient, through a third-party gadget PHP manager to install, you can easily switch IIS under the PHP version, this is not the main content of this article, here is not introduced;
PHP Manager for IIS 7 Download installation address: http://phpmanager.codeplex.com/
To write a small routine for a test:
<?php
$server = "127.0.0.1\sql2000";//server IP address, if local, can be written as localhost
$uid = "sa";//username
$pwd = "Yoooko"; /password
$database = "master";//Database name
//database connection
$conn =mssql_connect ($server, $uid, $pwd) or Die ("Connect Failed ");
mssql_select_db ($database, $conn);
Execute Query statement
$query = "SELECT * FROM CS";
$row =mssql_query ($query);
Print output query results while
($list =mssql_fetch_array ($row))
{
print_r ($list);
echo "<br>";
>
Run under IIS 7.5 The results are as follows: Success!
"5.6.11-> sql2008r2"
More than 5.6 of PHP has no php_mssql.dll, seemingly also no longer native support sql2000 above version of SQL Server, to pass the third party module, in the Microsoft website found:
Microsoft Drivers for PHP for SQL Server
http://www.microsoft.com/en-us/download/details.aspx?id=20098
Download link address has four files:
Sqlsrv30. Exe
sqlsrv31. Exe
sqlsrv32. Exe
Sqlsrv40. Exe
Support different PHP versions separately
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 is currently on the internet can be easily found;
(although 5.2 of such an old version should have been eliminated long ago, considering that some environments still need it)
Php_pdo_sqlsrv_52_ts_vc6.dll
Php_sqlsrv_52_ts_vc6.dll
1. My hand is PHP 5.6.11 Win32 TS, to use this
Php_sqlsrv_56_ts.dll
Php_pdo_sqlsrv_56_ts.dll
These two files, copied to the Php\ext directory,
2. In php.ini, add the following sentences:
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. Through the phpinfo can see the following module shows that our PHP should be able to access the SQL Server 2008r2 at this time;
5, write a small program to verify:
<?php
try {
$dbName = "Sqlsrv:server=127.0.0.1\sql2008r2;database=master";
$dbUser = "sa";
$dbPassword = "Yoooko";
$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 you receive the following prompt:
You may also need to install MICROSOFT®ODBC Driver for SQL server®-Windows
Download Address: https://www.microsoft.com/zh-cn/download/details.aspx?id=36434
If it is correct, it should be OK:
PS. Because the new module is PDO access to SQL Server 2008r2, if it is native access, it can be accessed directly through the IP address and the SA account.
If PHP and sql2008r2 on different computers, you have to set up a TCP port 1433来 access, see the second picture below, the dynamic port to 0, the port set to 14333 if your SQL2000 already occupied 1433, access to the address of the following add, XXXX port number
Sqlsrv:server=192.168.1.xxx\sql2008r2,14333;database=master
The above is a small set to introduce the PHP 5.6.11 access to SQL SERVER2008R2 Several situations, I hope to help you, if you have any questions please give me a message, small series will promptly reply to everyone. Here also thank you very much for the cloud Habitat Community website support!