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

Time:2019-12-11

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

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

Select the following files:

php_pdo_sqlsrv_7_ts_x64.dll

php_sqlsrv_7_ts_x64.dll

Take 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 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


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

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

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 php.in configuration file

X:\wamp\bin\apache\apache2.4.33\bin
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:

<?php
Header ("content type: text / HTML; charset = GBK"); // make the page code free
$hostname='192.168.1.215';
$dbname='ZD';
$username='Reader';
$password='TestReader';
//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) {
var_dump($row);
//print_r($row);
}
exit;

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: 127.0.0.1:6379> keys * 1) “kkk” 2) “key1” 127.0.0.1:6379> randomkey “key1” 127.0.0.1:6379> 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 […]