TP5 is the first to access the database and display the data on the template

adding data:

CREATE TABLE IF NOT EXISTS `think_data` (
  `id` int(8) unsigned NOT NULL AUTO_INCREMENT,
  `data` varchar(255) NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=MyISAM  DEFAULT CHARSET=utf8 AUTO_INCREMENT=4 ;
INSERT INTO `think_data` (`id`, `data`) VALUES
(1, 'onestopweb'),
(2, 'php'),
(3, 'tp5');

 

Configure database connection application/database.php

<?php
return [
    // database type
    'type'        => 'mysql',
    // server address
    'hostname'    => '127.0.0.1',
    // data storage name
    'database'    => 'tp5',
    // database username
    'username'    => 'tp5',
    // database password
    'password'    => 'C2S7a2c6',
    // database connection port
    'hostport'    => '',
    // database connection parameters
    'params'      => [],
    // The database encoding is utf8 by default
    'charset'     => 'utf8',
    // database table prefix
    'prefix'      => 'think_',
    // database debug mode
    'debug'       => true,
];

 

Configure the controller application\index\controller\Index.php

<?php
namespace app\index\controller;
use think\Controller;
use think\Db;
class Index extends Controller
{
    public function index()
    {
        $data = Db::name('data')->find();
        $this->assign('result', $data);
        return $this->fetch();
    }
}

 

Create HTML template application\index\view\index\index.html

<!DOCTYPE html>
<html lang="zh">
<head>
<meta charset="UTF-8">
<title>The first TP5 display data</title>
</head>
<body>
	<p>{$result.id}--{$result.data}</p>
</body>
</html>

 

configure web.config

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<system.webServer>
	<urlCompression doStaticCompression="true" doDynamicCompression="false" />
	<defaultDocument>
		<files>
			<clear />
			<add value="index.php" />
			<add value="index.html" />
			<add value="index.htm" />
			<add value="index.asp" />
		</files>
	</defaultDocument>
</system.webServer>
<system.webServer>
	<rewrite>
		<rules>
			<rule name="OrgPage" stopProcessing="true">
				<match url="^(.*)$" />
				<conditions logicalGrouping="MatchAll">
					<add input="{HTTP_HOST}" pattern="^(.*)$" />
					<add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
					<add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
				</conditions>
				<action type="Rewrite" url="index.php/{R:1}" />
			</rule>
		</rules>
	</rewrite>
</system.webServer>
</configuration>

 

Effect picture:

 

 

 

 

 

 

 

 

 

 

 

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326613317&siteId=291194637