Detailed explanation of thinkPHP data query common methods sample code summary

黄舟
Release: 2023-03-06 17:04:01
Original
1515 people have browsed it

This article mainly introduces the common methods of thinkPHP dataquery, and summarizes and analyzes the specific operation steps and related implementation skills of select, find, getField, query and other methods for database query operations in the form of examples. It needs Friends can refer to

. This article describes common methods of data query in thinkPHP. Share it with everyone for your reference, the details are as follows:

thinkphp has encapsulated commonly used query methods, and they are more practical. For uncommon queriesframework also retains the original Query methodquery.


$Model = new Model() // 实例化一个model对象 没有对应任何数据表
$Model->query("select * from think_user where status=1");
Copy after login

If you have just learned Thinkphp and don’t know much about the framework, you can use query($sql) and execute($sql) Two methods can implement any sql operation. query is used for query operations , execute is used for non-query operations . But the framework has encapsulated commonly used methods and is more convenient to use.

The following is the most commonly used query method:

1. select()


// 将所有数据查出,失败返回 false,无结果返回 null
$user = M('demo');
$data = $user->select();
dump($data);
// 加入条件
$user->field('name,sex')->where('id > 2')->order('age')->limit(3)->select();
//查询主键值为30的信息
$user->select('30');
// 查询主键为21,23,27的值
$user->select('21,23,27');
Copy after login

2. find()


// 查询出一条数据
$user = M('demo');
// 失败返回false
if($data = $user->find()){
  dump($data);
}
// 加入where条件
$user = M('demo');
$data = $user->field('name,sex')->where('id > 2')->find();
dump($data);
// 返回一维数组
$data->find('30');
$manager->where("username = '$username' and password = '$password'")->find();
Copy after login

3. getField()


##

// 获取列数据中的第一条
$user = M('demo');
$data = $user->getField('name');//默认第一个
// 第二个参数位true 则获取整列数据
$user->where("id = 3")->getField('name',true);
// 限制显示条数
$nickname = $User->where('status=1')->getField('nickname',8);
$nickname = $User->where('status=1')->limit(8)->getField('nickname',true);
// 返回二维数组,键名为第一个
$nickname = $User->where('status=1')->getField('id,nickname,sex');
// 使用连接符':' 键名是id值,键值则是account:nickname连接组成的字符串
$result = $User->where('status=1')->getField('id,account,nickname',':');
Copy after login
For detailed query methods, please refer to the "

Model>Query Statement" chapter in the ThinkPHP3.2 manual.

The above is the detailed content of Detailed explanation of thinkPHP data query common methods sample code summary. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template