Before, we implemented a project using the thinkphp5.1 framework.It realizes the connection between thinkphp5.1 and native sql server 2008 r2, and displays the data content on the page.

Native tools:1, compiling tool phpstorm

2. Wampserver integration tool (apache 2.4.33;php 7.0.29)

During the connection process,There are two ways to connect.

The first one (using thinkphp framework, configure the driver that comes with Microsoft):

First put the tp5 project in www under the wamp directory

Use 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 7.0.29 version, thread safety,64-bit.

Select the following files:



Put it


x:\ wamp \ bin \ php \ php7.0.29 \ ext

Then enter apache to modify the php.in configuration file

x:\ wamp \ bin \ apache \ apache2.4.33 \ bin

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 in the app directory and copy it to the database.php in the config under the index. Generally modify the database typeDatabase, username, password.

Then create a test.class.php file under the 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");
var_dump ($data);

Finally, you can access the entry file by calling it.


This method can also use the pdo_odbc method to connect to sqlserver (with thinkphp framework)

The second one (without going to thinkphp framework):

* (The above Microsoft-driven database method,(You can connect without thinkphp)

Use pdo_odbc to connect to the sql server database:

Enter apache to modify the php.in configuration file

x:\ wamp \ bin \ apache \ apache2.4.33 \ bin extension=php_pdo_odbc.dll Remove the colon in front,Turn on this service,Restart the apache server.

Create a test.php file in the www directory

code show as below:

header ("content-type:text/html;charset=gbk");//The page will not be garbled
//Connect using pdo_odbc
$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) {
var_dump ($row);
//print_r ($row);

Finally, you can access the entry file by calling it.

http://localhost:81/test.php is accessible

These methods should pay attention to:need to understand the use of db class thinkphp5.1 statement.Different connection methods can be set up according to different project requirements.

  • Previous PHP + WebUploader implement batch upload of pictures
  • Next How does springboot get static resources under relative path folder