add($data); Continuous operation $User = M("User"); // Instantiate User object $data['name'] = 'ThinkPHP';$data['email']"/> add($data); Continuous operation $User = M("User"); // Instantiate User object $data['name'] = 'ThinkPHP';$data['email']">
Home >Backend Development >PHP Tutorial >About ThinkPHP 5. Some basic operations of database
$User = M("User"); // 实例化User对象$data['name'] = 'ThinkPHP';$data['email'] = 'ThinkPHP@gmail.com';$User->add($data);
$User = M("User"); // 实例化User对象$data['name'] = 'ThinkPHP';$data['email'] = 'ThinkPHP@gmail.com';$User->data($data)->add();
$User = M("User"); // 实例化User对象 // 根据表单提交的POST数据创建数据对象$User->create();$User->add(); // 根据条件保存修改的数据
$User->addAll($data)
$User = M("User"); // 实例化User对象 // 查找status值为1的用户数据 以创建时间排序 返回10条数据$list = $User->where('status=1')->order('create_time')->limit(10)->select();
$User = M("User"); // 实例化User对象$condition['name'] = 'thinkphp';$condition['status'] = 1; // 把查询条件传入查询方法$User->where($condition)->select();
$User = M("User"); // 实例化User对象$condition['name'] = 'thinkphp';$condition['account'] = 'thinkphp';$condition['_logic'] = 'OR'; // 把查询条件传入查询方法$User->where($condition)->select();
$User = M("User"); // 实例化User对象 // 定义查询条件$condition = new stdClass(); $condition->name = 'thinkphp'; $condition->status= 1; $User->where($condition)->select();
$User = M("User"); // 实例化User对象 // 要修改的数据对象属性赋值$data['name'] = 'ThinkPHP';$data['score'] = array('exp','score+1');// 用户的积分加1$User->where('id=5')->save($data); // 根据条件保存修改的数据
$User = M("User"); // 实例化User对象$map['name|title'] = 'thinkphp'; // 把查询条件传入查询方法$User->where($map)->select();
$User = M("User"); // 实例化User对象$map['status&title'] =array('1','thinkphp','_multi'=>true); // 把查询条件传入查询方法$User->where($map)->select();
$map['id'] = array(array('gt',1),array('lt',10)) ;
$User = M("User"); // 实例化User对象 // 查找status值为1name值为think的用户数据 $User->where('status=1 AND name="think"')->find();
$User = M("User"); // 实例化User对象 // 获取ID为3的用户的昵称 $nickname = $User->where('id=3')->getField('nickname');
$this->getField('id',true); // 获取id数组
$User = M("User"); // 实例化User对象 // 获取所有用户的ID和昵称列表 $list = $User->getField('id,nickname');
$this->getField('id,name',5); // 限制返回5条记录
$Model->join(' work ON artist.id = work.artist_id')->join('card ON artist.card_id = card.id')->select();$Model->join('RIGHT JOIN work ON artist.id = work.artist_id')->select();
More query methods can be found here:
http://doc.thinkphp.cn/manual/query.html
$User = M("User"); // 实例化User对象 // 要修改的数据对象属性赋值$data['name'] = 'ThinkPHP';$data['email'] = 'ThinkPHP@gmail.com';$User->where('id=5')->save($data); // 根据条件保存修改的数据
$User = M("User"); // 实例化User对象 // 要修改的数据对象属性赋值$data['name'] = 'ThinkPHP';$data['email'] = 'ThinkPHP@gmail.com';//更新时间字段$data['time']=date("Y-m-d H:i:s", time());$User->where('id=5')->data($data)->save(); // 根据条件保存修改的数据
$User = M("User"); // 实例化User对象 // 更改用户的name值$User-> where('id=5')->setField('name','ThinkPHP');
$User = M("User"); // 实例化User对象 // 更改用户的name和email的值$data = array('name'=>'ThinkPHP','email'=>'ThinkPHP@gmail.com');$User-> where('id=5')->setField($data);
$User = M("User"); // 实例化User对象$User->where('id=5')->setInc('score',3); // 用户的积分加3$User->where('id=5')->setInc('score'); // 用户的积分加1$User->where('id=5')->setDec('score',5); // 用户的积分减5$User->where('id=5')->setDec('score'); // 用户的积分减1
$User = M("User"); // 实例化User对象$User->where('id=5')->delete(); // 删除id为5的用户数据$User->where('status=0')->delete(); // 删除所有状态为0的用户数据
$User->where('status=0')->order('create_time')->limit('5')->delete();
启动事务:$User->startTrans(); 提交事务:$User->commit(); 事务回滚:$User->rollback();
This article explains some basic operations about ThinkPHP 5. Database. For more related content, please pay attention to php Chinese website .
Related recommendations:
Rules for adding different data in thinkphp5
Related operations on ThinkPHP5 database
About ThinkPHP5 database and model usage
The above is the detailed content of About ThinkPHP 5. Some basic operations of database. For more information, please follow other related articles on the PHP Chinese website!