add($data); Continuous operation $User = M("User"); // Instantiate User object $data['name'] = 'ThinkPHP';$data['email']"> About ThinkPHP 5. Some basic operations of database-PHP Tutorial-php.cn

About ThinkPHP 5. Some basic operations of database

jacklove
Release: 2023-04-01 09:32:01
Original
1758 people have browsed it

New

Normal new

$User = M("User"); // 实例化User对象$data['name'] = 'ThinkPHP';$data['email'] = 'ThinkPHP@gmail.com';$User->add($data);
Copy after login

Continuous operation

$User = M("User"); // 实例化User对象$data['name'] = 'ThinkPHP';$data['email'] = 'ThinkPHP@gmail.com';$User->data($data)->add();
Copy after login

Use Create

$User = M("User"); // 实例化User对象 // 根据表单提交的POST数据创建数据对象$User->create();$User->add(); // 根据条件保存修改的数据
Copy after login

Batch insert data

$User->addAll($data)
Copy after login

Read Take the

select method

$User = M("User"); // 实例化User对象 // 查找status值为1的用户数据 以创建时间排序 返回10条数据$list = $User->where('status=1')->order('create_time')->limit(10)->select();
Copy after login

array as the query condition

$User = M("User"); // 实例化User对象$condition['name'] = 'thinkphp';$condition['status'] = 1; // 把查询条件传入查询方法$User->where($condition)->select();
Copy after login

Change the logical relationship of multiple conditions

$User = M("User"); // 实例化User对象$condition['name'] = 'thinkphp';$condition['account'] = 'thinkphp';$condition['_logic'] = 'OR'; // 把查询条件传入查询方法$User->where($condition)->select();
Copy after login

Object mode query

$User = M("User"); // 实例化User对象 // 定义查询条件$condition = new stdClass(); $condition->name = 'thinkphp'; $condition->status= 1; $User->where($condition)->select();
Copy after login

Expression query

$User = M("User"); // 实例化User对象 // 要修改的数据对象属性赋值$data['name'] = 'ThinkPHP';$data['score'] = array('exp','score+1');// 用户的积分加1$User->where('id=5')->save($data); // 根据条件保存修改的数据
Copy after login

Quick or query

$User = M("User"); // 实例化User对象$map['name|title'] = 'thinkphp'; // 把查询条件传入查询方法$User->where($map)->select();
Copy after login

Quick multi-field and query

$User = M("User"); // 实例化User对象$map['status&title'] =array('1','thinkphp','_multi'=>true); // 把查询条件传入查询方法$User->where($map)->select();
Copy after login

Interval query

$map['id'] = array(array('gt',1),array('lt',10)) ;
Copy after login

find method

$User = M("User"); // 实例化User对象 // 查找status值为1name值为think的用户数据 $User->where('status=1 AND name="think"')->find();
Copy after login

Limit field

$User = M("User"); // 实例化User对象 // 获取ID为3的用户的昵称 $nickname = $User->where('id=3')->getField('nickname');
Copy after login

Return an array composed of one field

$this->getField('id',true); // 获取id数组
Copy after login

Multiple fields return an associative array

$User = M("User"); // 实例化User对象 // 获取所有用户的ID和昵称列表 $list = $User->getField('id,nickname');
Copy after login

Limit the number of returns

$this->getField('id,name',5); // 限制返回5条记录
Copy after login

left join

$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();
Copy after login

More query methods can be found here:
http://doc.thinkphp.cn/manual/query.html

Update

Update after condition selection

$User = M("User"); // 实例化User对象 // 要修改的数据对象属性赋值$data['name'] = 'ThinkPHP';$data['email'] = 'ThinkPHP@gmail.com';$User->where('id=5')->save($data); // 根据条件保存修改的数据
Copy after login

Continuous operation

$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(); // 根据条件保存修改的数据
Copy after login

Update specific fields

$User = M("User"); // 实例化User对象 // 更改用户的name值$User-> where('id=5')->setField('name','ThinkPHP');
Copy after login

Update multiple fields

$User = M("User"); // 实例化User对象 // 更改用户的name和email的值$data = array('name'=>'ThinkPHP','email'=>'ThinkPHP@gmail.com');$User-> where('id=5')->setField($data);
Copy after login

Increase and decrease

$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
Copy after login

Delete

Conditional deletion

$User = M("User"); // 实例化User对象$User->where('id=5')->delete(); // 删除id为5的用户数据$User->where('status=0')->delete(); // 删除所有状态为0的用户数据
Copy after login

Limit the number of deletions

$User->where('status=0')->order('create_time')->limit('5')->delete();
Copy after login

Transaction

启动事务:$User->startTrans(); 提交事务:$User->commit(); 事务回滚:$User->rollback();
Copy after login

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!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!