Analysis on the method of connecting PHP to SQL Server [based on thinkphp5.1 framework]


An example of this article describes how PHP connects to SQL server. To share with you for your reference, as follows:

Before that, we implemented the project matching thinkphp5.1 framework, realized the connection between thinkphp5.1 and native SQL Server 2008 R2, and displayed the data content on the page.

Tools of this machine: 1. Compiling tool phpstorm

2. Wampserver integration tool (APACHE 2.4.33; PHP 7.0.29)

There are two ways to connect during the connection process.

The first one (use the ThinkPHP framework to configure the driver provided by Microsoft):

First, put the TP5 project in the WWW under the Wamp directory

Using PHP and SQL Server driver package provided by Microsoft


Download: sqlsrv40.exe (download the corresponding driver according to the corresponding version of PHP)

After downloading and decompressing, select the corresponding PHP version information, such as: the native PHP is version 7.0.29, thread safe, 64 bit.

Select the following files:



Take it



Then enter Apache to modify the configuration file


Add between extension = PHP? Gd2.dll and extension = PHP? Gettext.dll


Then restart the Apache server.

Open the TP5 project with phpstorm, create a config file under the index template, and then create the database.php configuration database. You can refer to the database.php format under the app application directory, copy it to the database.php of config under the index. Generally, modify the database type, database, user name, password.

Next, create a test.class.php file under the controller controller. The code is as follows

namespace app\index\controller;
use think\Db;
use think\Controller;
class Test extends Controller
public function zz(){
$data= Db::query('SELECT top 100 * FROM V_XZ_SPZD_KC');

Finally, you can access the entry file.


This method can also be used to connect SQL Server with PDO ﹣ ODBC method (with the think PHP framework)

The second one (without thinking PHP framework):

*(the above method of Microsoft driving database can be connected without ThinkPHP)

To connect to the SQL Server database with PDO ODBC:

Enter Apache to modify the configuration file

takeextension=php_pdo_odbc.dllRemove the previous colon, start the service, and restart the Apache server.

Create a test.php file in the WWW directory

The code is as follows:

Header ("content type: text / HTML; charset = GBK"); // make the page code free
//Connect using PDO? ODBC mode
$dbDB = new PDO("odbc:Driver={SQL Server};Server=$hostname;Database=$dbname", $username, $password);
$sql = "SELECT top 100 * FROM V_DY_SPZD";
foreach ($dbDB->query($sql) as $row) {

Finally, you can access the entry file.

http://localhost:81/test.phpCan access

These methods should be noted that you need to understand the usage statements of the DB class of thinkphp5.1. Different connection methods can be established according to different project requirements.

For more information about ThinkPHP, readers who are interested in it can see the following topics: introduction to ThinkPHP, summary of operation skills of ThinkPHP template, summary of common methods of ThinkPHP, introduction to CodeIgniter, advanced tutorial of CI (CodeIgniter) framework, introduction to Zend framework and technical summary of PHP template.

I hope that this article will be helpful for you to design your PHP program based on the think PHP framework.

Recommended Today

[Redis5 source code learning] analysis of the randomkey part of redis command

baiyan Command syntax Command meaning: randomly return a key from the currently selected databaseCommand format: RANDOMKEY Command actual combat:> keys * 1) “kkk” 2) “key1”> randomkey “key1”> randomkey “kkk” Return value: random key; nil if database is empty Source code analysis Main process The processing function corresponding to the keys command is […]