This article mainly introduces ThinkPHP query statements and associated query usage, and common query methods in the form of instances, including techniques such as using arrays as query conditions and object methods to query, for more information about ThinkPHP, see ThinkPHP.
This article describes the ThinkPHP query statement and associated query usage. Share it with you for your reference. The details are as follows:
On the thinkphp framework page, you can directly spell SQL query statements to perform database query read/write operations. The following describes the examples.
In addition to string query conditions, the query conditions for arrays and objects are very common. these are essential for basic queries.
I. use arrays as the query conditions
The code is as follows:
$ User = M ("User"); // instantiate the User object
$ Condition ['name'] = 'thinkphp'; // pass the query condition to the query method
$ User-> where ($ condition)-> select ();
II. query using objects any object can be used. here, the stdClass built-in object is used as an example.
The code is as follows:
$ User = M ("User"); // instantiate the User object
// Define the query condition $ condition = new stdClass ();
$ Condition-> name = 'thinkphp'; // query records whose name value is thinkphp
$ User-> where ($ condition)-> select (); // The preceding query condition is equivalent to where ('name = "thinkphp "') object-based query and array-based query have the same effect and are optional.
Common queries with where conditions
1. string format
The code is as follows:
$ User = M ('user ');
$ List = $ user-> where ('Id> 5 and id <9')-> select ();
$ List = $ user-> where ($ data)-> select ();
2. array form
The code is as follows:
$ User = M ('user ');
$ List = $ user-> where (array ('username' => 'Www .bitsCN.com ')-> select ();
$ List = $ user-> where ($ data)-> select ();
3. object form
The code is as follows:
$ User = M ('user ');
$ A = new stdClass ();
$ A-> username = 'www .bitsCN.com;
$ List = $ user-> where ($ a)-> select ();
Join queries for two tables:
The code is 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
The code is as follows:
$ User = M ('user ');
$ Data ['id'] = array ('GT ', 20), array ('Lt', 23), 'and ');
$ List = $ user-> where ($ data)-> select ();
Combined query
The code is 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
The code is 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 ();
Association query of three data tables
The code is 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 ();
Data table query conditions
① The following describes how to directly store the query conditions in the where clause, which facilitates the writing of the conditions.
The code is 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 method.
The code is as follows:
$ M_product = M ('product ');
$ Map ['pid '] = $ proid;
$ P_result = $ M_product-> where ($ map)-> select ();
I hope this article will help you with ThinkPHP framework programming.