tp51框架的数据库操作

原创 2018-12-21 17:02:50 514
摘要:数据库配置return [     // 数据库类型     'type'            => 'mysql',  

数据库配置

return [
    // 数据库类型
    'type'            => 'mysql',
    // 服务器地址
    'hostname'        => '127.0.0.1',
    // 数据库名
    'database'        => 'php',
    // 用户名
    'username'        => 'root',
    // 密码
    'password'        => 'root',
    // 端口
    'hostport'        => '3306',
    // 连接dsn
    'dsn'             => '',
    // 数据库连接参数
    'params'          => [],
    // 数据库编码默认采用utf8
    'charset'         => 'utf8',
    // 数据库表前缀
    'prefix'          => '',
    // 数据库调试模式
    'debug'           => true,
    // 数据库部署方式:0 集中式(单一服务器),1 分布式(主从服务器)


原生查询

<?php
namespace app\index\controller;
use think\Db;
class Demo
{
    public function select()
    {
        // dump(\think\Db::query('select *from staff'));
        // $sql = "SELECT name,salary FROM staff WHERE salary>5000 LIMIT 5";
        // 顺序占位符
        // $sql = "SELECT name,salary FROM staff WHERE salary>? LIMIT ?";
        // $res = Db::query($sql,[5000,3]);

        $sql = "SELECT name,salary FROM staff WHERE salary>:salary LIMIT :num";
        //给参数值传递第二个参数:PDO参数常量,进行类型限定
        $res = Db::query($sql,['num'=>[4,\PDO::PARAM_INT],'salary'=>[5000,\PDO::PARAM_INT]]);
        dump($res);
    }

    public function update()
    {
        $sql = "UPDATE staff SET salary = :salary WHERE staff_id = :staff_id ;";
        Db::execute($sql,['salary'=>[6500,\PDO::PARAM_INT], 'staff_id'=>[10, \PDO::PARAM_INT]]);
        return '更新成功';
    }
}

查询构造器查询

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

class Query
{
    public function find()
    {
        $res = Db::table('staff')->find(10);
        dump($res);

        $res = Db::table('staff')
            // ->where('staff_id','=',11)
            ->field(['name as 姓名,sex as 性别,age as 年龄'])
            ->where('staff_id',11)
            ->find();
        dump($res);
    }
    public function select()
    {
        $res = Db::table('staff')
            ->field(['name as 姓名,salary as 工资'])
            // ->where('salary > 3000')
            ->where('salary','>','3000')
            ->order('salary','DESC')
            ->limit(5)
            ->select();
        dump($res);
    }

    public function insert()
    {
        // $data = [
        //     'name'=>'杨建设',
        //     'sex'=> 0,
        //     'age'=> 25,
        //     'salary'=>4000
        // ];
        // $num = Db::table('staff')->insert($data);
        // $id = Db::getLastInsID();
        // return $num ? '成功,id='.$id : '没有记录被添加';
        $data = [
            ['name'=>'大雄','sex'=>0,'age'=>12,'salary'=>1900],
            ['name'=>'静香','sex'=>1,'age'=>11,'salary'=>3300],
            ['name'=>'胖虎','sex'=>0,'age'=>13,'salary'=>2200],
            ['name'=>'小夫','sex'=>0,'age'=>14,'salary'=>4400]
        ];
        $num = Db::table('staff')->data($data)->insertAll();
        return $num ? '添加成功'.$num.'条记录~~' : '没有记录被添加';
    }

    public function update()
    {
        //更新必须是基于前置查询,不允许无条件更新
        //使用update(),终极方法
        //将工资小于等于4000的员工,加薪1000
        $num = Db::table('staff')
            ->where('salary','<=',4000)
            ->data(['salary'=>Db::raw('salary+1000')])
            ->update();

        return $num ? '更新成功'.$num.'条记录~~' : '没有记录被更新';

    }

    public function delete()
    {
        //删除必须基于前置查询,不允许无条件删除
        //删除用delete()
        // $num = Db::table('staff')->delete(16);
        

        // $num = Db::table('staff')
        //     ->where('salary','>=',5000)
        //     ->delete();

        $num = Db::table('staff')->delete(true);

        return $num ? '删除成功'.$num.'条记录~~' : '没有记录被删除';
    }
}


批改老师:韦小宝批改时间:2018-12-21 17:04:49
老师总结:写的很不错!前期对数据库的比较重要的基本上就是增删查改!

发布手记

热门词条