Home >PHP Framework >ThinkPHP >Detailed explanation of how to query data and print in ThinkPHP5
In recent years, with the rapid development of Internet technology, Web development has become a technology widely used in various fields, and PHP, as a server-side programming language, has been widely used in the field of Web development. In PHP, ThinkPHP is a powerful, easy-to-use web development framework that has become the first choice of many developers. This article will focus on the methods of querying data and printing under the ThinkPHP5 framework.
In ThinkPHP5, use the assistant function db()
to operate the database. There are many ways to query data, the most commonly used The ones are select()
and find()
.
select()
The method queries multiple pieces of data and returns a two-dimensional array containing multiple arrays. find()
The method queries a piece of data and returns a one-dimensional array. For example, query all data in the user
table:
use think\facade\Db; $users = Db::table('user')->select();
Query id
in the user
table is 1 data:
$user = Db::table('user')->where('id', 1)->find();
ThinkPHP5 supports chain operations. You can use other methods directly after one method to filter out the required data more conveniently. For example, query the top 10 data in the user
table where status
is 1 and sorted in descending order by create_time
:
$users = Db::table('user') ->where('status', 1) ->order('create_time', 'desc') ->limit(10) ->select();
In addition to basic queries, ThinkPHP5 also provides some advanced query syntax to make querying data more convenient.
2.1 Callback query
Callback query is a chain operation method. It uses the where()
method to pass in an anonymous function and uses the query condition as the The parameters of the function, its execution result is the query condition, and then a query builder object is returned. For example, query all data in the user
table where name
is equal to leijun
or email
is equal to leijun@gmail.com
:
$users = Db::table('user')->where(function($query){ $query->where('name', 'leijun') ->whereOr('email', 'leijun@gmail.com'); })->select();
2.2 like
Query
like
Query is a fuzzy query method, which uses the where()
method Pass in a string with like
as the condition, and use the query condition as a parameter of the string, and then return a query builder object. For example, query all data in the user
table where name
starts with leijun
:
$users = Db::table('user')->where('name', 'like', 'leijun%')->select();
2.3 in
Query
in
Query is a way to query in a set of data. It is implemented using the whereIn()
method. This method accepts a field name and an array as parameters. Returns a query builder object. For example, query all the data in [1,2,3] for id
in the user
table:
$users = Db::table('user')->whereIn('id', [1,2,3])->select();
Through the above query method, we have obtained the data we want, and then we need to print the data.
The data printing method of ThinkPHP5 is very simple, we only need to use the dump()
or var_dump()
function. For example, to print all the data in the queried user
table:
use think\facade\Db; $users = Db::table('user')->select(); dump($users);
Open this page in the browser, and the queried data will be printed.
Through the introduction of this article, you have learned how to query data and print under the ThinkPHP5 framework. Among them, we mainly introduce basic query, advanced query and Data printing. With this knowledge, I believe you can already perform efficient data query and printing during development.
The above is the detailed content of Detailed explanation of how to query data and print in ThinkPHP5. For more information, please follow other related articles on the PHP Chinese website!