Home > PHP Framework > ThinkPHP > Detailed explanation of how to query data and print in ThinkPHP5

Detailed explanation of how to query data and print in ThinkPHP5

PHPz
Release: 2023-04-17 11:01:08
Original
1696 people have browsed it

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.

  1. Basic query

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();
Copy after login

Query id in the user table is 1 data:

$user = Db::table('user')->where('id', 1)->find();
Copy after login

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();
Copy after login
  1. Advanced query

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();
Copy after login

2.2 likeQuery

likeQuery 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();
Copy after login

2.3 inQuery

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();
Copy after login
  1. Data printing

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);
Copy after login

Open this page in the browser, and the queried data will be printed.

  1. Conclusion

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!

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