Analysis of the Method of Connecting PHP to SQL Server Based on thinkPHP 5.1 Framework

  • 2021-12-09 08:36:34
  • OfStack

In this paper, the method of connecting PHP to SQL and Server is described as an example. Share it for your reference, as follows:

Previously, we implemented the project with thinkPHP5.1 framework, realized the connection between thinkPHP5.1 and native sql server 2008 R2, and displayed the data content on the page.

Tools used by this machine: 1. Compilation tool PhpStorm

2. WampServer Integration Tools (Apache 2.4. 33; PHP 7.0. 29)

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

Type 1 (using ThinkPHP framework, configured with Microsoft's own driver):

First, put the project of tp5 in www under wamp directory

Using PHP and SQL Server driver packages 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)

Select the corresponding PHP version information after downloading and decompressing. For example, the PHP of this machine is version 7.0. 29, thread-safe and 64-bit.

Select the following file:

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 go to 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 tp5 project with PhpStorm, create an config file under index template and then create database. php configuration database. You can refer to database. php format under app application directory, copy it in database. php under index, and modify database type, database, user name and password.

Then set up a test. class. php file under 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, it can be accessed by calling the entry file.

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

This method can also be used to connect sqlserver with pdo_odbc method (with thinkPHP framework)

Type 2 (without going to the ThinkPHP framework):

* (The above Microsoft-driven database method can be connected without thinkPHP)

Connect the SQL Server database with PDO_odbc:

Enter Apache to modify php. in configuration file

X:\wamp\bin\apache\apache2.4.33\bin
Will extension=php_pdo_odbc.dll Remove the previous colon, start this service and restart the Apache server.

Create an test. php file in the www directory

The code is as follows:


<?php
header("Content-type: text/html; charset=gbk");// Make the page not garbled 
$hostname='192.168.1.215';
$dbname='ZD';
$username='Reader';
$password='TestReader';
// Use PDO_ODBC Mode connection 
$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, it can be accessed by calling the entry file.

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

One thing to note about these methods is that you need to know the use statements of the DB class in thinkPHP 5.1. Different connection methods can be formed according to different project requirements.

For more readers interested in thinkPHP related contents, please check the topics of this site: "Introduction to ThinkPHP", "Summary of thinkPHP Template Operation Skills", "Summary of Common Methods of ThinkPHP", "Introduction to codeigniter", "Advanced Course of CI (CodeIgniter) Framework", "Introduction to Zend FrameWork Framework" and "Summary of PHP Template Technology".

I hope this article is helpful to the PHP programming based on ThinkPHP framework.


Related articles: