数据库增删改查 利用TP5.1框架的MVC结构实现。
model文件
<?php
namespace app\index\model;
use think\Model;
class User1 extends Model
{
}controller文件
<?php
namespace app\index\controller;
use app\index\model\User1;
class Demo7
{
//数据库模型查找单条数据
public function get()
{
$res=User1::Where('id','=',50)
->field(['id'=>'学号','name'=>'姓名'])
->find();
dump($res);
}
//数据库模型查找多条数据
public function select()
{
$res=User1::Where('id','<',50)
->select();
dump($res);
}
//数据库模型更新数据
public function updata()
{
$res=User1::Where('id','=',1)
->update(['name'=>'哇咔']);
dump($res);
}
//数据库模型插入数据
public function insert()
{
$data=[
'name'=>'挖挖',
'email'=>'wawa@php.cn',
'password'=>sha1(123)];
$res=User1::insert($data);
dump($res);
}
//数据库模型删除数据
public function delete()
{
$res=User1::Where('id','=',82)
->delete();
dump($res);
}
}
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号