0、开启调试模式(WWW\tp5\application\config.php)
'app_debug' =>true,
1、连接数据库(...database.php)
// 数据库类型
'type' => 'mysql',
// 服务器地址
'hostname' => '127.0.0.1',
// 数据库名
'database' => 'testphp',
// 用户名
'username' => 'root',
// 密码
'password' => 'root',
2、控制器中书写代码(...application\index\controller\Index.php)
<?php
namespace app\index\controller;
//引入系统数据类
use think\Db;
//引入系统控制类
use think\Controller;
class Index extends Controller
{
public function index()
{
//从数据库中读取数据
$data=Db::table('user')->select();
//分配数据给页面
$this->assign('data',$data);
//加载页面
return view();
}
}
3、页面中(...index\view\index\index.html)
{volist name="data" id="value"}
<tr>
<td>{$value.id}</td>
<td>{$value.name}</td>
<td>{$value.pass}</td>
</tr>
{/volist}
二:数据库 增删改查
<?php namespace app\index\controller; use think\Db; class Index { public function index() { return '<style type="text/css">*{ padding: 0; margin: 0; } .think_default_text{ padding: 4px 48px;} a{color:#2E5CD5;cursor: pointer;text-decoration: none} a:hover{text-decoration:underline; } body{ background: #fff; font-family: "Century Gothic","Microsoft yahei"; color: #333;font-size:18px} h1{ font-size: 100px; font-weight: normal; margin-bottom: 12px; } p{ line-height: 1.6em; font-size: 42px }</style><div style="padding: 24px 48px;"> <h1>:)</h1><p> ThinkPHP V5<br/><span style="font-size:30px">十年磨一剑 - 为API开发设计的高性能框架</span></p><span style="font-size:22px;">[ V5.0 版本由 <a href="http://www.qiniu.com" target="qiniu">七牛云</a> 独家赞助发布 ]</span></div><script type="text/javascript" src="https://tajs.qq.com/stats?sId=9347272" charset="UTF-8"></script><script type="text/javascript" src="https://e.topthink.com/Public/static/client.js"></script><think id="ad_bd568ce7058a1091"></think>'; } //查询 public function query(){ $date=Db::Query("select * from user where id=?",[2]); dump($date); echo Db::getLastSql(); } //增 public function insert(){ //$data=Db::execute("insert into user value(null,'user1','321')"); //$data=Db::execute("insert into user value(null,?,?)",['user2','12345']); $data=Db::execute("insert into user value(null,:name,:pass)",['name'=>'user3','pass'=>'12345']); dump($data); } //删除 public function deletes(){ //$data=Db::execute("delete from user where id=?",[3]); //$data=Db::execute("delete from user where id=2"); $data=Db::execute("delete from user where id=:id",['id'=>8]); dump($data); } //修改 public function update(){ //$data=Db::execute("update user set pass=? where id=?",["888",9]); $data=Db::execute("update user set pass=? where id=?",["666",9]); $data=Db::execute("update user set pass=:pass where id=:id",['pass'=>"555",'id'=>10]); dump($data); } }