Home>

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

Address:https://www.microsoft.com/en-us/download/details.aspx?id=20098

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:

php_pdo_sqlsrv_7_ts_x64.dll

php_sqlsrv_7_ts_x64.dll

Put it

Place

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

extension=php_gd2.dll

extension=php_sqlsrv_7_ts_x86.dll

extension=php_pdo_sqlsrv_7_ts_x86.dll

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

<?php
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.

http://localhost:81/1111/tp5/public/index/test/zz

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:

<?php
header ("content-type:text/html;charset=gbk");//The page will not be garbled
$hostname="192.168.1.215";
$dbname="zd";
$username="reader";
$password="testreader";
//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);
}
exit;

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.

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