The examples in this article summarize the thinkPHP query methods. Share it with everyone for your reference, the details are as follows:
1. Common query methods
1. Use string query;
Copy code The code is as follows: $m->where(' id=1 and name="roge" ')->find();
One 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();
2. Expression query
EQ is equal to;
NEQ is not equal;
GT Greater than;
EGT is greater than or equal to;
LT is less than;
ELT 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,
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();
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用于对数据进行写入
For more information related to thinkPHP, please view the special topics on this site: "Introduction to ThinkPHP Tutorial" and "Summary of Common Methods of ThinkPHP"
I hope this article will be helpful to everyone’s PHP programming based on the thinkPHP framework.