<?php
namespace app\index\controller;
use think\Db;
class Index
{
public function index()
{
return ;
}
//单条查询
public function find(){
$res = Db::table('reader')
->where('id','>',1) //id大于1
->find();
dump($res);
}
//多条查询方法
public function select(){
$res = Db::table('reader')
//指定字段
->field(['name','age','tel','email'])
//->field(['name'=>'姓名','age'=>'年龄','tel'=>'电话','email'=>'邮箱'])
->where('id','>=','2')
->order('name','DESC')
->select();
dump($res);
}
//新增记录
public function insert(){
//添加一条记录
$data =['name'=>'孙一','age'=>32,'tel'=>'15926897575','email'=>'15926897575@qq.com','salary'=>3500];
$res = Db::table('reader')
->data($data)
->insert();
echo $res ? '添加'.$res.'条记录成功!' :'添加失败';
}
//新增多条记录
public function insertAll(){
//添加多条记录
$data =[
['name'=>'胡中','age'=>36,'tel'=>'13586892126','email'=>'13586892126@qq.com','salary'=>3500],
['name'=>'长国','age'=>21,'tel'=>'15895656563','email'=>'15895656563@qq.com','salary'=>3500],
['name'=>'至国','age'=>38,'tel'=>'13698366535','email'=>'13698366535@qq.com','salary'=>3500],
];
$res = Db::table('reader')
->data($data)
->insertAll();
echo $res ? '添加'.$res.'条记录成功!' :'添加失败';
}
public function update(){
//更新记录操作
$res = Db::table('reader')
->data(['salary'=>Db::raw('salary+500')])
->where('salary','<=',3000)
->update();
echo $res ? '更新'.$res.'条记录成功!' :'更新失败';
}
public function delete(){
//删除记录
$res = Db::table('reader')
->where('salary','>=',4000)
->delete();
echo $res ? '删除'.$res.'条记录成功!' :'删除失败';
}
}