onestopweb 阅读(130) 评论(0)

添加数据:

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');

 

配置数据库连接 application/database.php

<?php
return [
    // 数据库类型
    'type'        => 'mysql',
    // 服务器地址
    'hostname'    => '127.0.0.1',
    // 数据库名
    'database'    => 'tp5',
    // 数据库用户名
    'username'    => 'tp5',
    // 数据库密码
    'password'    => 'C2S7a2c6',
    // 数据库连接端口
    'hostport'    => '',
    // 数据库连接参数
    'params'      => [],
    // 数据库编码默认采用utf8
    'charset'     => 'utf8',
    // 数据库表前缀
    'prefix'      => 'think_',
    // 数据库调试模式
    'debug'       => true,
];

 

配置控制器 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();
    }
}

 

创建HTML模板 application\index\view\index\index.html

<!DOCTYPE html>
<html lang="zh">
<head>
<meta charset="UTF-8">
<title>第一个TP5显示数据</title>
</head>
<body>
	<p>{$result.id}--{$result.data}</p>
</body>
</html>

 

配置web.config

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

 

效果图:

 

 

 

 

 

 

 

 

 

 

 

  • 大小: 5.8 KB