博主信息
博文 56
粉丝 3
评论 1
访问量 60890
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
thinkphp数据库操作——2018年5月24日
沈斌的博客
原创
1399人浏览过

thinkphp的数据库操作,使用链式写法。

文件路径:tp/application/index/controller/Test.php

访问其中的方法时,使用 tp.top/index.php/index/test/update,查看执行的结果

实例

<?php
namespace app\index\controller;
use think\Db;

class Test
{
    public function find(){
        $res = Db::table('staf')
            ->field('name,sex,salary')
            ->where('staff_id',2)
            ->find();
        dump($res);
    }

    public function select()
    {
        $res=Db::table('staf')
            ->field('name,sex,salary')
            ->where('salary','>',3000)
            ->order('salary','DESC')
            ->limit(5)
            ->select();
        dump($res);
    }

    public function insert()
    {
        $data=[
            'name'=>'halc',
            'sex'=>1,
            'age'=>45,
            'salary'=>6800
        ];

        $num=Db::table('staf')->data($data)->insert();
        $id=Db::getLastInsId();
        return $num ? '添加,id='.$id : '没有被添加';

    }

    public function insertMulti()
    {
        $data=[
            ['name'=>'jla','sex'=>0,'age'=>35,'salary'=>7800],
            ['name'=>'alice','sex'=>0,'age'=>45,'salary'=>8800],
            ['name'=>'beal','sex'=>1,'age'=>50,'salary'=>9800],
        ];

        $num = DB::table('staf')->data($data)->insertAll();
        return $num ? '添加成功'.$num.'条记录':'没有记录添加';
    }

    public function update()
    {
        $num = Db::table('staf')
            ->where('salary','<',3600)
            ->data(['salary'=>Db::raw('salary+1000')])
            ->update();

        $num1=Db::table('staf')
            ->update(['sex'=>1,'staff_id'=>9]);
        return $num ? '更新成功'.$num.'条记录' :'没有记录更新';
    }

    public function delete()
    {
        $num=Db::table('staf')->delete(12);
        return $num ? '删除了'.$num.'条记录' :'没有删除';

    }
}
?>

运行实例 »

点击 "运行实例" 按钮查看在线实例


批改状态:未批改

老师批语:
本博文版权归博主所有,转载请注明地址!如有侵权、违法,请联系admin@php.cn举报处理!
全部评论 文明上网理性发言,请遵守新闻评论服务协议
0条评论
作者最新博文
关于我们 免责申明 意见反馈 讲师合作 广告合作 最新更新 English
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习
PHP中文网抖音号
发现有趣的

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号

  • 登录PHP中文网,和优秀的人一起学习!
    全站2000+教程免费学