Summary of thinkPHP query methods

WBOY
Release: 2016-07-29 09:07:12
Original
1211 people have browsed it

The examples in this article summarize the thinkPHP query method. Share it with everyone for your reference, the details are as follows:

1. Ordinary query method

1. Use string query;

Copy the code The code is as follows:

$m->where(' id=1 and name="roge" ')->find();


A disadvantage of this method is that when the query field in the data table is a string, quotation marks need to be added to the field value.

2. Using arrays (recommended)

$data['name']="adfa";
$data['id']=3;
$data['_logic']="or"; //字段之间的逻辑关系,默认为and的关系
$m->where($data)->find();

Copy after login

2. Expression query

EQ is equal to;
NEQ is not equal to;
GT is greater than;
EGT is greater than or equal to;
LT is less than;
ELT is less than or equal to;
LIKE Fuzzy query;

$data['id']=array('gt',6);
$data['name']=array('like','%as%'); //notlike
//$data['name']=array('like',array('%as%','%ts'),'and'); 默认为or关系,如果用and需要明确指定
$m->where($data)->select();
//其他查询 between, not between (之间有空格),in,not between,

Copy after login

3. Interval query

$data['id']=array(array('gt',5),array('lt',10)); //默认生成的是and的关系
//$data['id']=array(array('lt',5),array('gt',10),'or')
$data['name']=array(array('like','%d%'),array('like','%e%'),'gege','or');
$m->where($data)->select();

Copy after login

4. Statistical query

count, max, min, avg, sum

Copy code The code is as follows:

$m- >max('id')


5. SQL direct query

$m=M();
$result=$m->query("select * from think_user where id>1")
//query主要用于对数据进行读取
$result=$m->execute("insert into think_user(`name`) values ('dfd') ");
//execute用于对数据进行写入

Copy after login

For more information related to thinkPHP, please check out the special topics on this site: "ThinkPHP Getting Started Tutorial" and "Summary of Common Methods of ThinkPHP"

I hope this article will explain It will be helpful for everyone to design PHP programs based on the thinkPHP framework.

The above is a summary of thinkPHP query methods, including relevant aspects. I hope it will be helpful to friends who are interested in PHP tutorials.

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
Popular Tutorials
More>
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!