安装(tp 5.1)
composer create-project topthink/think=5.1.36
简单使用
查询学生表的名称
sql
CREATE TABLE `student` (
`id` INT UNSIGNED PRIMARY KEY AUTO_INCREMENT,
`name` VARCHAR(10) NOT NULL UNIQUE DEFAULT '',
`gender` TINYINT UNSIGNED NOT NULL DEFAULT 0,
`email` VARCHAR(128) NOT NULL DEFAULT '',
`mobile` VARCHAR(20) NOT NULL DEFAULT '',
`entry_date` DATE NOT NULL
) ENGINE=INNODB DEFAULT CHARSET=utf8;
INSERT INTO `student` VALUES
(1, 'Allen4', 0, 'a@qq.com', '1231233123', '2019-01-03'),
(2, 'Allen1', 1, 'b@qq.com', '1231243123', '2019-01-03'),
(3, 'Allen2', 1, 'c@qq.com', '1231235123', '2019-01-04'),
(4, 'Allen3', 0, 'd@qq.com', '1231236123', '2019-01-03');
修改tp5 数据库配置文件
/config/database.php
逻辑代码:\application\index\controller\Index.php
public function student() {
// $student = \think\Db::query('SELECT `name` FROM `student`');
$student = \think\Db::name('student')->field('name')->select();
$this->assign('data', $student);
return $this->fetch();
}
模板文件: \application\index\view\index\student.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>学生列表</title>
</head>
<body>
{foreach $data as $v}
{$v.name}<br/>
{/foreach}
</body>
</html>
访问:http://127.0.0.1:8082/index.php/index/index/student
打开调试模式 :
\config\app.php
在访问时页面下方有调试信息
apache配置
<VirtualHost *:80>
##ServerAdmin webmaster@dummy-host2.example.com
DocumentRoot "G:\web\wamp\www\think\public"
##ServerName dummy-host2.example.com
ErrorLog "logs/tp-error.log"
CustomLog "logs/tp-access.log" common
<Directory "G:\web\wamp\www\think\public">
#
# Possible values for the Options directive are "None", "All",
# or any combination of:
# Indexes Includes FollowSymLinks SymLinksifOwnerMatch ExecCGI MultiViews
#
# Note that "MultiViews" must be named *explicitly* --- "Options All"
# doesn't give it to you.
#
# The Options directive is both complicated and important. Please see
# http://httpd.apache.org/docs/2.4/mod/core.html#options
# for more information.
#
Options +FollowSymLinks -MultiViews
#
# AllowOverride controls what directives may be placed in .htaccess files.
# It can be "All", "None", or any combination of the keywords:
# AllowOverride FileInfo AuthConfig Limit
#
AllowOverride All
#
# Controls who can get stuff from this server.
#
<IfModule rewrite_module>
Require all granted
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ index.php/$1 [QSA,PT,L]
</IfModule>
</Directory>
</VirtualHost>