The example of this article describes the thinkphp query and the associated query usage. Share to everyone for your reference. Specifically as follows:
In the thinkphp frames page we can directly spell the SQL query statement to implement the database query read and write operations, the following is an example to illustrate this.
Ordinary query In addition to the string query criteria, array and object mode query conditions are very common, these are the basic query must master.
Using an array as a query condition
Copy Code code as follows:
$User = M ("User"); Instantiating the User object
$condition [' name '] = ' thinkphp '; Passing query criteria into the Query method
$User->where ($condition)->select ();
Two, use object way to query can use any object here to Stdclass built-in objects for example
Copy Code code as follows:
$User = M ("User"); Instantiating the User object
Define query Criteria $condition = new StdClass ();
$condition->name = ' thinkphp '; Query name's value is thinkphp record
$User->where ($condition)->select (); The above query condition is equivalent to where (' name= ' thinkphp ') the effect of querying and using an array query is the same, and it can be
Normal query with where condition
1. String form
Copy Code code as follows:
$user =m (' user ');
$list = $user->where (' id>5 and Id<9 ')->select ();
$list = $user->where ($data)->select ();
2. Array form
Copy Code code as follows:
$user =m (' user ');
$list = $user->where (Array (' username ' => ' www.jb51.net '))->select ();
$list = $user->where ($data)->select ();
3. Object Form
Copy Code code as follows:
$user =m (' user ');
$a =new stdClass ();
$a->username= ' www.jb51.net;
$list = $user->where ($a)->select ();
Two-table associated query:
Copy Code code as follows:
$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
Copy Code code as follows:
$user =m (' user ');
$data [' ID ']=array (Array (' GT ', '), array (' LT ', #), ' and ');
$list = $user->where ($data)->select ();
Group Query
Copy Code code as follows:
$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
Copy Code code as follows:
$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 ();
Three data table associated query
Copy Code code as follows:
$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 for data tables
① below is the direct bar. The query's condition is placed in the where, which facilitates the writing of the condition.
Copy Code code as follows:
$m _test = m ("Product");
$productmeaage = $m _test->where ("p_id= ' $proid '")->select ();
② In addition to the above method there is also an array of ways
Copy Code code as follows:
$M _product = M (' product ');
$map [' pid '] = $proid;
$p _result = $M _product->where ($map)->select ();
I hope this article will be helpful to everyone's thinkphp framework program design.