标签:必须 this table span title code end thinkphp public
<?php
namespace app\index\controller;
use think\Controller;
use think\db; //必须引用
class Index extends Controller
{
public function index()
{
/*查询*/
//db()助手函数
$userName = db(‘user_list‘)->field(‘user_name‘)->select();
//table方法必须指定完整的数据表名
$userInfo22 = db::table(‘wts_user_list‘)
->where(array(‘user_id‘=>22))->find();
//name方法
$userId = db::name(‘user_list‘)->field(‘user_id‘)->select();
//Query对象
$query = new \think\db\Query();
$userTitle = $query->name(‘user_list‘)->column(‘user_title‘);
/*更新*/
db::name(‘bulletin_list‘)->where(‘bulletin_id‘,67)
->update([‘bulletin_title‘=>‘新闻‘]);
// 更新数据表中的数据
db(‘user‘)->where(‘id‘,1)->update([‘name‘ => ‘thinkphp‘]);
// 更新某个字段的值
db(‘user‘)->where(‘id‘,1)->setField(‘name‘,‘thinkphp‘);
// 自增 score 字段
db(‘user‘)->where(‘id‘, 1)->setInc(‘score‘);
// 自减 score 字段
db(‘user‘)->where(‘id‘, 1)->setDec(‘score‘);
/*删除*/
// 根据主键删除
Db::table(‘think_user‘)->delete(1);
Db::table(‘think_user‘)->delete([1,2,3]);
// 条件删除
Db::table(‘think_user‘)->where(‘id‘,1)->delete();
Db::table(‘think_user‘)->where(‘id‘,‘<‘,10)->delete();
// 根据主键删除
db(‘user‘)->delete(1);
// 条件删除
db(‘user‘)->where(‘id‘,1)->delete();
/*添加*/
$data = [‘foo‘ => ‘bar‘, ‘bar‘ => ‘foo‘];
Db::table(‘think_user‘)->insert($data);
$data = [
[‘foo‘ => ‘bar‘, ‘bar‘ => ‘foo‘],
[‘foo‘ => ‘bar1‘, ‘bar‘ => ‘foo1‘],
[‘foo‘ => ‘bar2‘, ‘bar‘ => ‘foo2‘]
];
Db::name(‘user‘)->insertAll($data);$data = [
[‘foo‘ => ‘bar‘, ‘bar‘ => ‘foo‘],
[‘foo‘ => ‘bar1‘, ‘bar‘ => ‘foo1‘],
[‘foo‘ => ‘bar2‘, ‘bar‘ => ‘foo2‘]
];
Db::name(‘user‘)->insertAll($data);
return $this->fetch(‘index‘);
}
}
---------------------
作者:羊崽
来源:CSDN
原文:https://blog.csdn.net/ynw1990/article/details/77646359
标签:必须 this table span title code end thinkphp public
原文地址:https://www.cnblogs.com/weixiaofantasy/p/9966083.html