ThinkPHP Query Statement and Associated Query Usage Instance

  • 2021-07-24 10:29:02
  • OfStack

This paper illustrates the usage of ThinkPHP query statement and associated query. Share it for your reference. The details are as follows:

In the thinkphp framework page, we can directly spell the sql query statement to realize the read-write operation of database query, which is illustrated by an example below.

In addition to string query criteria, array and object query criteria are very common in ordinary queries, which are necessary for basic queries.

1. Use arrays as query criteria

$User = M("User"); // Instantiation User Object 
$condition['name'] = 'thinkphp'; //  Passing query criteria into query methods 
$User->where($condition)->select();

2. Query by Object. You can use any object. Take stdClass built-in objects as an example

$User = M("User"); //  Instantiation User Object 
// Define query criteria $condition = new stdClass();
$condition->name = 'thinkphp';  // Query name The value of is thinkphp Records of
$User->where($condition)->select(); //  The above query condition is equivalent to where('name="thinkphp"') Querying by object has the same effect as querying by array, and can be

Ordinary query with where condition

1. String form

$user=M('user');
$list=$user->where('id>5 and id<9')->select();
$list=$user->where($data)->select();

2. Array form

$user=M('user');
$list=$user->where(array('username'=>'www.ofstack.com'))->select();
$list=$user->where($data)->select();

3. Object form

$user=M('user');
$a=new stdClass();
$a->username='www.ofstack.com;
$list=$user->where($a)->select();

An associated query for two tables:

$M_shopping = M('Shops'); 
$M_product = M('Product');
$list_shops = $M_shopping->join('as shops left join hr_product as product on shops.product_id = product.p_id')
->field('product.p_id,product.p_name,shops.product_amount,shops.product_id')
->where("shops.user_cookie='".$_COOKIE['hr_think_userid']."'")
->group('shops.id')
->select();

Interval query

$user=M('user');
$data['id']=array(array('gt',20),array('lt',23),'and');
$list=$user->where($data)->select();

Composite query

$user=M('user');
$data['username']='pengyanjie';
$data['password']=array('eq','pengyanjie');
$data['id']=array('lt',30);
$data['_logic']='or';
$list=$user->where($data)->select();
dump($list);

Compound query

$user=M('user');
$data['username']=array('eq','pengyanjie');
$data['password']=array('like','p%');
$data['_logic']='or';
$where['_complex']=$where;
$where['id']=array('lt',30);
$list=$user->where($data)->select();

Association query of 3 data tables

$M_shopping = M('Shops');
$M_product = M('Product');
$M_proimg = M('Product_image');
$list_shops = $M_shopping->join('as shops left join hr_product as product on shops.product_id = product.p_id left join
hr_product_image as productimgon productimg.p_id = product.p_id')->fiel('productimg.pi_url,product.p_id,product.p_name,shops.product_amount,shops.product_id,product.am_id,
product.p_procolor,product.p_price,product_amount*p_price as totalone')->where("shops.user_cookie='".$_COOKIE['hr_think_userid']."'")
->group('shops.id')->select();

Query criteria of data table

① The following is directly put the query conditions into where, which is convenient for writing conditions

$User = M("User"); //  Instantiation User Object 
// Define query criteria $condition = new stdClass();
$condition->name = 'thinkphp';  // Query name The value of is thinkphp Records of
$User->where($condition)->select(); //  The above query condition is equivalent to where('name="thinkphp"') Querying by object has the same effect as querying by array, and can be
0

② In addition to the above methods, there is another way to use arrays

$M_product = M('Product');
$map['pid'] = $proid;
$p_result = $M_product->where($map)->select();

I hope this article is helpful to everyone's ThinkPHP framework programming.


Related articles: