Blogger Information
Blog 51
fans 3
comment 1
visits 35958
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
数据库,查询构造器中常用方法—2018年5月24日12时48分
Gee的博客
Original
603 people have browsed it

实例

<?php
namespace app\index\controller;

use think\Db;

class Homework0524
{
	public function demo()
	{
		$res = Db::table('staff')
			->field(['name'=>'姓名', 'sex'=>'性别', 'salary'=>'工资'])
			->where('salary','>',3000)
			->limit(6)
			->order('salary', 'DESC')
			->select();
		
		dump($res);
	}

	public function insert()
	{
		// $data = [
		// 	'name' => '归海一刀',
		// 	'sex' => 0,
		// 	'salary' => 6600,
		// 	'age' => 38
		// ];
		// $num = Db::table('staff')->data($data)->insert();
		// return $num ? '增加成功,id是'.Db::getLastInsID() : '没有数据被增加';

		$data2 = [
			['name'=>'吕布','sex'=>0,'age'=>32,'salary'=>8900],
			['name'=>'貂蝉','sex'=>1,'age'=>26,'salary'=>7300],
			['name'=>'甄宓','sex'=>1,'age'=>22,'salary'=>4600],
		];
		$num2 = Db::table('staff')->data($data2)->insertAll();
		return $num2 ? '成功增加'.$num2.'条数据' : '没有数据被增加';
	}

	public function update()
	{
		$num = Db::table('staff')
			->where('staff_id', 9)
			->data(['salary'=> Db::raw('salary+1500')])
			->update();

		return $num ? '更新成功,添加了'.$num.'条记录' : '没有记录被更新';
	}

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

		return $num ? '删除成功,添加了'.$num.'条记录' : '没有记录被删除';
	}
}

运行实例 »

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


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