知识点:
第一部分 数据库基础
第二部分 基本使用
第三部分 查询构造器
第一部分 TP5数据库基础
- TP5内置抽象数据库访问层,把不同的数据库操作封装起来;
- 需要使用公共的Db类进行访问
- 目前包含了Mysql,SqlServer、PgSQL、Sqlite等数据库的支持
配置数据库
有四种方法:
- 配置文件定义 — config -> database.php
- 方法配置 — 调用Db类的时候动态定义连接信息
- 模型类定义 — 模型类里定义connection属性
- 字符串连接定义格式:
第二部分 基本使用
添加数据
- insert()方法
- insertAll()方法
- getLastInsID()方法
用于直接使用数据库运行原生的SQL操作,记不得tp5的操作了就可以用这个:
- query()方法:查
- execute()方法:增删改
- getLastInsID()方法:获取最后更新的id
- getNumRows()方法:获取影响的行数
更新数据:
- update()方法
- where()方法
- setField()方法
- setInc()方法
- setDec()方法
聚合查询
- count()方法
- sum()方法
- avg()方法
- min()方法
- max()方法
删除数据库
- delete()方法
数据集
- isEmpty()方法
- all()方法
- toArray()方法
查询数据
- find() 方法
- select() 方法
第三部分 查询构造器
选择数据库的方法:
Db::table()
Db::table()
要选择后才能使用TP5的那些方法,记得使用数据库的时候要导入库。
EXAMPLE
实现增、删、改、查的操作,注意里面的参数是如何传递的。
index.php
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 |
<?php namespace app\index\controller; use think\Controller; use think\Db; class Index extends Controller { public function create() { // echo 'test'; return $this->fetch(); } public function store() { // 1.拿到post过来的数据 $username = $this -> request -> post('username'); $password = md5($this -> request -> post('password')); $sex = $this -> request -> post('sex'); echo $username.'------'.$password.'--------'.$sex; // 2.写入数据库 Db::execute('INSERT users(`username`,`password`,`sex`) VALUES(:username,:password,:sex)',['username'=>$username, 'password'=>$password, 'sex'=>$sex]); } public function show() { // 从数据库把数据读过来 $user = Db::query('SELECT `id`,`username`,`sex` FROM `users`'); $this -> assign('user',$user); return $this -> fetch(); } public function delete() { $id = $this -> request -> param('id'); $users = Db::table('users')->where('id','eq',$id)->delete(); $this -> redirect('index/index/show'); } public function edit() { $id = $this -> request -> param('id'); $users = Db::table('users')->find($id); print_r($users); $this -> assign('users',$users); return $this -> fetch(); } public function modify() { // 1.把post过来的数据拿到 $data['username'] = $this -> request -> post('username'); $data['password'] = md5($this -> request -> post('password')); $data['sex'] = $this -> request -> post('sex'); // 2.拿到用户的id $id = $this -> request -> param('id'); // 3.更新数据库 Db::table('users') -> where('id','eq',$id)->update($data); $this -> redirect('index/index/show'); } } |
show.html:显示数据
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
<body> <h1 align="center">用户表</h1> <table align="center" border="1" cellspacing="0" text-align="center"> <tr height="50px"> <td width="200">ID</td> <td width="300">用户名</td> <td width="100">用户性别</td> <td width="100">操作</td> </tr> {foreach $user as $v} <tr> <td>{$v.id}</td> <td>{$v.username}</td> <td>{if condition="$v.sex==0"}男{else if condition="1"}女{/if}</td> <td><a href="{:url('index/index/edit',['id'=>$v.id],'')}">编辑</a>|<a href="{:url('index/index/delete',['id'=>$v.id],'')}">删除</a></td> </tr> {/foreach} </table> </body> |
create.html:插入数据库
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
<body> <div style="text-align: center;"> <h1>用户注册</h1> <form action="{:url('index/index/store'),'',''}" method="post"> <table style="margin: 0 auto;"> <tr> <td width="100px">用户名</td> <td><input type="text" name="username"></td> </tr> <tr> <td>密码</td> <td><input type="password" name="password"></td> </tr> <tr> <td>性别</td> <td>男:<input type="radio" name="sex" value="0" checked> 女:<input type="radio" name="sex" value="1"> </td> </tr> <tr> <td> </td> <td><input type="submit" value="确认添加"></td> </tr> </table> </form> </div> </body> |
edit.html:修改数据库
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
<body> <div style="text-align: center;"> <h1>编辑用户</h1> <form action="{:url('index/index/modify',['id'=>$users.id],'')}" method="post"> <table style="margin: 0 auto;"> <tr> <td width="100px">用户名</td> <td><input type="text" name="username" value="{$users.username}"></td> </tr> <tr> <td>密码</td> <td><input type="password" name="password"></td> </tr> <tr> <td>性别</td> <td> {if condition="$users.sex==0"} 男:<input type="radio" name="sex" value="0" checked> 女:<input type="radio" name="sex" value="1"> {else if condition="$users.sex==1"} 男:<input type="radio" name="sex" value="0"> 女:<input type="radio" name="sex" value="1" checked> {/if} </td> </tr> <tr> <td> </td> <td><input type="submit" value="确认修改"></td> </tr> </table> </form> </div> </body> |