Blogger Information
Blog 28
fans 0
comment 0
visits 16430
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
数据库查询10条-2018年05月25日01:22
植树青年小江同志的博客
Original
698 people have browsed it

table(),field(),order(), where(), limit(),使用

实例

<?php
namespace app\index\controller;

use think\db;


class Query
{
    // table(),field(),order(), where(), limit(),使用

    public function select()
    {
        $res = Db::table('staff')
                ->field(['name' => '姓名', 'age' => '年龄', 'salary' => '工资'])
                ->where('staff_id', '>' , 3)
                ->order('salary DECS')
                ->limit(7)
                ->select();
                dump($res);
                
    }
}

运行实例 »

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


返回结果:

array(7) {
 [0] => array(3) {
   ["姓名"] => string(6) "杨康"
   ["年龄"] => int(28)
   ["工资"] => int(4000)
 }
 [1] => array(3) {
   ["姓名"] => string(9) "欧阳峰"
   ["年龄"] => int(30)
   ["工资"] => int(4000)
 }
 [2] => array(3) {
   ["姓名"] => string(6) "关羽"
   ["年龄"] => int(53)
   ["工资"] => int(5000)
 }
 [3] => array(3) {
   ["姓名"] => string(12) "还珠格格"
   ["年龄"] => int(18)
   ["工资"] => int(5000)
 }
 [4] => array(3) {
   ["姓名"] => string(9) "武大郎"
   ["年龄"] => int(25)
   ["工资"] => int(5000)
 }
 [5] => array(3) {
   ["姓名"] => string(6) "武松"
   ["年龄"] => int(25)
   ["工资"] => int(5000)
 }
 [6] => array(3) {
   ["姓名"] => string(6) "刘备"
   ["年龄"] => int(58)
   ["工资"] => int(5000)
 }
}

insert(),数据打包data()

实例

 public function insert()
    { 
        $data = [
            'name' => '李小璐',
            'age' =>'33',
            'sex' => 1,
            'salary' => '1000000',

        ];


        $res = Db::table('staff')
                ->data($data)
                ->insertGetId();

        

        return $res ? '添加成功' . 'id=' . $res  : '添加失败';
    }

运行实例 »

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

insertAll()

实例

public function insertAll()
    {
        $data = [
            ['name' => '贾乃亮', 'age' => 30, 'sex' => 0, 'salary' => 1],
            ['name' => 'PGOne', 'age' => 28, 'sex' => 0, 'salary' => 999],

        ];

        $res = Db::table('staff')
                ->data($data)
                ->insertAll();

                return $res ? '添加成功' . $res : '添加失败';
    }

运行实例 »

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

update()

实例

public function update()
    {
        $res= Db::table('staff')
                ->where('name', '=', 'PGOne')
                ->data(['salary'=>'0'])
                ->update();

        return $res ? '添加成功' . $res : '添加失败';
    }

运行实例 »

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

delete()

实例

 public function delete()
    {
        $res = Db::table('staff')
                ->where('staff_id', '>' , 31)
                ->delete();

        return $res ? '添加成功' . $res : '添加失败';
    }

运行实例 »

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

实例

public function insertGetId()
    { 
        $data = [
            'name' => '李小璐',
            'age' =>'33',
            'sex' => 1,
            'salary' => '1000000',

        ];


        $res = Db::table('staff')->insertGetId($data);

        $id = Db::getLastInsID();

        return $res ? '添加成功' . 'id=' . $id  : '添加失败';
    }

运行实例 »

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

获取自增主键

实例

public function insertGetId()
    { 
        $data = [
            'name' => '李小璐',
            'age' =>'33',
            'sex' => 1,
            'salary' => '1000000',

        ];


        $res = Db::table('staff')->insertGetId($data);

        $id = Db::getLastInsID();

        return $res ? '添加成功' . 'id=' . $id  : '添加失败';
    }

运行实例 »

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


Correction status:Uncorrected

Teacher's comments:
Statement of this Website
The copyright of this blog article belongs to the blogger. Please specify the address when reprinting! If there is any infringement or violation of the law, please contact admin@php.cn Report processing!
All comments Speak rationally on civilized internet, please comply with News Comment Service Agreement
0 comments
Author's latest blog post
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!