This article describes the common methods of thinkPHP data query. Share it for your reference, as follows:
Thinkphp has encapsulated commonly used query methods, and they are all more practical, and it is also retained for infrequently used query frameworks.Original query methodquery
。
$Model = new Model() // Instantiate a model object without any data table$Model->query("select * from think_user where status=1");
If you just learn Thinkphp and don't know much about the framework, you can use itquery($sql)
andexecute($sql)
Two methods can implement any SQL operation.query
Used for query operations,execute
For non-query operations. However, the framework has been packaged with common methods and is more convenient to use.
Here are the most commonly used query methods:
1. select()
// Find all data, return false if failed, return null if no result$user = M('demo'); $data = $user->select(); dump($data); // Addition conditions$user->field('name,sex')->where('id > 2')->order('age')->limit(3)->select(); //Query information with primary key value of 30$user->select('30'); // Query the value of the primary key 21, 23, 27$user->select('21,23,27');
2. find()
// Query a data$user = M('demo'); // Failed to return falseif($data = $user->find()){ dump($data); } // Add where conditions$user = M('demo'); $data = $user->field('name,sex')->where('id > 2')->find(); dump($data); // Return a one-dimensional array$data->find('30'); $manager->where("username = '$username' and password = '$password'")->find();
3. getField()
// Get the first column data$user = M('demo'); $data = $user->getField('name');//The first one is default// The second parameter bit is true to obtain the entire column of data$user->where("id = 3")->getField('name',true); // Limit the number of displayed characters$nickname = $User->where('status=1')->getField('nickname',8); $nickname = $User->where('status=1')->limit(8)->getField('nickname',true); // Returns a two-dimensional array with the key name first$nickname = $User->where('status=1')->getField('id,nickname,sex'); // Use the connector ':' key name is the id value, and the key value is a string composed of account:nickname connection$result = $User->where('status=1')->getField('id,account,nickname',':');
For detailed query methods, please refer to the "Model>Query Statement" chapter in the ThinkPHP3.2 manual.
For more information about thinkPHP related content, please check out the topic of this site:ThinkPHP Introduction Tutorial》、《Summary of the operation skills of thinkPHP templates》、《Summary of common methods of ThinkPHP》、《Codeigniter Introductory Tutorial》、《Advanced tutorial on CI (CodeIgniter) framework》、《Zend FrameWork Framework Introduction Tutorial》、《Basic tutorial on getting started with smarty templates"and"PHP template technical summary》。
I hope that the description in this article will be helpful to everyone's PHP programming based on the ThinkPHP framework.