With the rapid development of the Internet, the development of Web applications is becoming more and more complex. And paging query is one of the common functions in web applications. ThinkPHP6 is a web framework that helps developers develop applications quickly. In this article, we will discuss how to perform paginated queries using MySQL in ThinkPHP6.
First, we need to create a database in ThinkPHP6. The statement to create a database in MySQL is as follows:
CREATE DATABASE `thinkphp6`;
Next, we need to create a data table. In MySQL, we can create a data table using the following command:
CREATE TABLE `user` ( `id` int(11) NOT NULL AUTO_INCREMENT, `username` varchar(255) NOT NULL, `email` varchar(255) NOT NULL, PRIMARY KEY (`id`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
This command will create a data table named "user". The data table contains three fields: "id", "username" and "email". Among them, "id" is the primary key.
Next, we need to configure the database connection in ThinkPHP6. We find the database.php file in the config folder and open it. In this file, we need to configure the database connection information, including database type, host name, user name, password and database name. We can use the following code to set the database connection information:
<?php return [ 'type' => 'mysql', 'hostname' => '127.0.0.1', 'database' => 'thinkphp6', 'username' => 'root', 'password' => '', 'hostport' => '', 'dsn' => '', 'params' => [], 'charset' => 'utf8mb4', 'collation' => 'utf8mb4_general_ci', 'prefix' => '', 'debug' => true, 'deploy' => 0, 'rw_separate' => false, 'master_num' => 1, 'slave_no' => '', 'read_master' => false, 'fields_strict' => true, 'resultset_type' => 'array', 'auto_timestamp' => false, 'sql_explain' => false, ];
Next, we will demonstrate how to use MySQL in ThinkPHP6 for paging queries. We will use data from thinkphp6 data table for demonstration.
Using ThinkPHP6 to perform paging queries is very simple. We just need to use the paginate method in the model. This method accepts two parameters: "$listRows" and "$simple". "$listRows" indicates the number of rows in each page; "$simple" indicates whether it is simple mode. We can use the following code in our controller:
<?php namespace appindexcontroller; use thinkController; class UserController extends Controller { public function index() { $listRows = 10; // 每页显示的行数 $user = model('User'); $list = $user->paginate($listRows); return view('index', ['list' => $list]); } }
The above code will use model('User') to query the database table named "User" and display the number of rows per page in the controller Set to 10 rows and use the paginate method for paginated query.
We use the following code to display data in the view file:
<?php foreach ($list as $item) { echo $item['id'] . ' ' . $item['username'] . ' ' . $item['email'] . '<br/>'; } // 显示分页 echo $list->render(); ?>
The above code will traverse the result set and display the data in the view file, and finally display it using the $list->render() method Pagination links.
Summary:
In this article, we discussed how to use MySQL for paginated queries in ThinkPHP6. We first created a database and then created a data table named "User". Next, we configured the database connection and implemented pagination query using the paginate method in the model. Finally, we display the query results in the view file and use the $list->render() method to display the paginated links. I hope readers can implement the paging query function in their own applications based on the content of this article.
The above is the detailed content of How to use Mysql for paging query in ThinkPHP6. For more information, please follow other related articles on the PHP Chinese website!