Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to understand the CURD of thinkphp

2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article mainly introduces "how to understand the CURD of thinkphp". In daily operation, I believe many people have doubts about how to understand the CURD of thinkphp. I have consulted all kinds of materials and sorted out simple and easy operation methods. I hope to help you answer the doubts about "how to understand the CURD of thinkphp"! Next, please follow the small series to learn together!

Read of data

The copy code is as follows:

$m=new Model('User');

$m=M('User');

select

$m->select();//Get all data, return as an array

find

$m->find($id);//get a single data

getField(field name)//Get a specific field value

$arr=$m->where('id=2')->getField('username');

ThinkPHP 3: Creating Data (Focus)

Add to data Create

The copy code is as follows:

$m=new Model('User');

$m=M('User');

$m-> Field Name = Value

$m->add();

The return value is the newly added id number

ThinkPHP 3 Delete Data (Focus)

The copy code is as follows:

$m=M('User');

$m->delete(2); //delete data with id = 2

$m->where ('id = 2')->delete(); //Same effect as above, also deletes data with id = 2

The return value is the number of affected rows

ThinkPHP 3 update data (focus)

The copy code is as follows:

$m=M('User');

$data['id']=1;

$data['username']='ztz2';

$m->save($data);

The return value is the number of affected rows

============================================

I. Common inquiry methods

II. Expression query mode

III. Interval query

IV. Statistical inquiry

5. SQL direct query

I. Common inquiry methods

a. String

The copy code is as follows:

$arr=$m->where("sex=0 and username='gege'")->find();

b, array

The copy code is as follows:

$data['sex']=0;

$data['username']='gege';

$arr=$m->where($data)->find();

Note: This method defaults to an and relationship. If you use an or relationship, you need to add array values.

The copy code is as follows:

$data['sex']=0;

$data['username']='gege';

$data['_logic']='or';

II. Expression query mode

The copy code is as follows:

$data['id']=array('lt',6);

$arr=$m->where($data)->select();

EQ equals

NEQ is not equal to

GT greater than

EGT greater than or equal to

LT less than

ELT less than or equal to

LIKE Fuzzy Query

The copy code is as follows:

$data['username']=array('like','%ge');

$arr=$m->where($data)->select();

NOTLIKE

$data ['username ']=array ('notlike','%ge %'); //notlike without spaces

$arr=$m->where($data)->select();

Note: If a field matches multiple wildcards

The copy code is as follows:

$data ['username ']=array ('like',array ('% ge %','%2%','% five %'),'and');//if there is no third value, the default relationship is or

$arr=$m->where($data)->select();

BETWEEN

$data['id']=array('between',array(5,7));

$arr=$m->where($data)->select();

//SELECT * FROM `tp_user` WHERE ( (`id` BETWEEN 5 AND 7 ) )

$data['id']=array ('not between', array(5,7));//Note that there must be a space between not and between

$arr=$m->where($data)->select();

IN

$data['id']=array('in',array(4,6,7));

$arr=$m->where($data)->select();

//SELECT * FROM `tp_user` WHERE ( `id` IN (4,6,7) )

$data['id']=array('not in',array(4,6,7));

$arr=$m->where($data)->select();

//SELECT * FROM `tp_user` WHERE ( `id` NOT IN (4,6,7) )

III. Interval query

The copy code is as follows:

$data ['id ']=array(array ('gt',4),array ('lt ',10));//default relation is and relation

//SELECT * FROM `tp_user` WHERE ( (`id` > 4) AND (`id`

< 10) ) $data['id']=array(array('gt',4),array('lt',10),'or') //关系就是or的关系 $data['name']=array(array('like','%2%'),array('like','%五%'),'gege','or'); 四、统计查询 count //获取个数 max //获取最大数 min //获取最小数 avg //获取平均数 sum //获取总和 五、SQL直接查询 a、query 主要数处理读取数据的 成功返回数据的结果集 失败返回boolean false 复制代码 代码如下: $m=M(); $result=$m->

query("select * from t_user where id >50");

var_dump($result);

b, execute for updating a write operation

Number of affected rows returned successfully

Failure returns boolean false

The copy code is as follows:

$m=M();

$result=$m->execute("insert into t_user(`username`) values('ztz3')");

var_dump($result);

At this point, the study of "how to understand CURD of thinkphp" is over, hoping to solve everyone's doubts. Theory and practice can better match to help everyone learn, go and try it! If you want to continue learning more relevant knowledge, please continue to pay attention to the website, Xiaobian will continue to strive to bring more practical articles for everyone!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report