Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use PHP+MySQL to achieve paging

2025-03-13 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

Editor to share with you how to use PHP+MySQL to achieve paging, I believe that most people do not know much about it, so share this article for your reference, I hope you can learn a lot after reading this article, let's go to know it!

Environmental preparation

Using PHP technology, the best partner is AMP (Apache,MySQL,PHP). There are many integrated environments, such as WAMP,XAMPP,phpnow and so on. But today I'm going to manually build a working environment for PHP.

Apache

First of all, we need to download Apache server from Apache's website. It is best to download the msi version, so that we do not have to manually configure a variety of environments.

Apache download address: a msi version of ApacheServer, the first choice for quickly building a PHP server environment.

MySQL

MySQL, a well-known open source project in the database world, has now been acquired by Oracle, and I don't know if it will charge in the future. But for now, the best choice for PHP development is MySQL. There is no need to say much about this. The download address is attached.

MySQL download address

Remember to keep your user name and password in mind during installation.

PHP

Some people say that PHP is not a language, but a framework, a client implementation that connects to MySQL. I thought about it carefully, and it seemed to make some sense. But if you put it this way, there are many languages that are not languages. As a civilian hero, php is well known for his progress. The download address of php is attached below so that you don't have to find it alone.

PHP download address: msi version of PHP, you can quickly build php without manual configuration of the environment.

Working environment

After we have installed the above three software, we can officially start to build the environment. For now, all we need to know is that our working directory is under the htdocs folder of Apache. Htdocs, as a virtual directory, is maintained by the apache configuration file, which we will come across later.

Remember to be in the htdocs folder under the installation directory of apache.

Database preparation

Although the environment has been set up, but if you want to do paging. First of all, we have to have the data. "A skillful wife cannot make bricks without rice". Now let's prepare the data.

Build a database

Database building statement create database my_database_name

Here you can use the mysql database that comes with MySQL installation. It's a little easier.

Build a table

The data warehouse is still built, and now we have to "separate rooms", that is, data storage places, tables.

Create table table_name ()

Also here, in order to be lazy, use your own database tables. The details are as follows:

Mysql > use mysqlDatabase changedmysql > desc innodb_table_stats +-- +-- + | Field | Type | Null | Key | Default | Extra | +-- +-- + | database _ name | varchar (64) | NO | PRI | NULL | | table_name | varchar (64) | NO | PRI | NULL | | last_update | timestamp | NO | | CURRENT_TIMESTAMP | on update CURRENT_TIMESTAMP | | n_rows | bigint (20) unsigned | NO | | NULL | | clustered_index_size | bigint (20) unsigned | NO | | NULL | sum_of_other_index_sizes | bigint (20) unsigned | NO | NULL | | +- -+

Pre-stored data

For the convenience of demonstration, we need to store some data in advance. It doesn't matter whether you use bulk import or manual addition. The core or

Insert into table_name () values ()

For example, the data we have stored is as follows:

Mysql > select * from innodb_table_stats +-+ | database_name | | table_name | last_update | n_rows | clustered_index_size | sum_of_other_index_sizes | +-- -fams | admin | 2016-07-19 14:47:02 | 3 | 1 | 0 | fams | 2016-07-14 14:42:44 | 2 | 1 | 3 | fams | 2016-07-14 20:14:31 | 4 | 1 | 3 | fams | class | 2016-07-14 14:36:02 | 3 | 1 | 0 | | fams | dog | 2016-08-11 15:25:50 | 4 | 1 | 0 | fams | 2016-07-14 15:55:09 | 6 | 1 | 2 | fams | sub_class | 2016-07-14 14:38:51 | 8 | 1 | 1 | 1 | fams | user | 2016-07-14 14:15:59 | 2 | 1 | 0 | mysql | gtid_executed | 2016-07-14 12:50:25 | 0 | 1 | 0 | | privilegesystem | privilege | 2016-08-08 08:56:21 | 3 | 1 | 0 | privilegesystem | role | 2016-08-08 08:26:56 | 2 | 1 | privilegesystem | role_privilege | 2016-08-08 09:51:04 | 2 | 1 | 1 | privilegesystem | user | 2016-08-08 11:07:35 | 2 | 1 | 0 | privilegesystem | user_role | 2016-08-08 11:08:15 | 2 | 1 | 2 | | | sys | sys_config | 2016-07-14 12:50:30 | 6 | 1 | 0 | | test | datetest | 2016-07-19 10:02:38 | 2 | 1 | 0 | +-| -+-+ 16 rows in set (0.00 sec)

PHP expansion preparation

If it is a non-msi version of the installation, we need to manually open the extension of PHP, so that we can use some functions of mysql to manipulate the database.

Php.ini

The file is located in the installation directory of php. We need to remove the semicolon in front of the following code.

[PHP_MYSQL]

Extension=php_mysql.dll

[PHP_MYSQLI]

Extension=php_mysqli.dll

In the ini file of PHP, the comment is

Paging principle

"everything is ready but the east wind." now let's talk about the core idea of paging.

That is the current page, page size, total number of records. These three through the total number of records and page size, we can calculate the total number of pages. Then realize the corresponding display according to the current page.

Total number of records

/ / get the total number of records $sql_total_records = "select count (*) from innodb_table_stats"; $total_records_result = mysql_query ($sql_total_records); $total_records = mysql_fetch_row ($total_records_result); echo "total number of records:". $total_records [0]. "

"

Current page

/ / obtain the page number accessed by the client through GET $current_page_number = isset ($_ GET ['page_number'])? $_ GET [' page_number']: 1 if ($current_page_number$total_pages) {$current_page_number = $total_pages;} echo "the page number to be accessed is:". $current_page_number

Paging core

/ / after obtaining the page to be visited and the page size, start paging $begin_position = ($current_page_number-1) * $page_size;$sql = "select * from innodb_table_stats limit $begin_position,$page_size"; $result = mysql_query ($sql)

So we can get the result set we want. The next thing is how to show it on the page.

Page display

/ / process the result set echo "Mysql Fixed Assets Table"; echo "DbNameTableNameLast_updaten_NowsClustered_Index_SizeSum_od_Other_Index_sizes"; while (($row = mysql_fetch_row ($result)) {echo "; echo". $row [0]. ""; echo "". $row [1]. ""; echo "". $row [2]. ""; echo ". $row [3]."; echo ". $row [4]."; echo ". $row [5]." Echo ";} echo"; / / cycle to show the total number of pages? >

The initial page of the result is:

Click the page number

next page

The above is all the content of the article "how to use PHP+MySQL to achieve paging". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report