Comparison of several ways to display data

xiaoxiao2021-03-06  39

When used to display database data, generally use the cyclic body to do, the common method has two statements, and let's talk about their usage in different situations.

Let's introduce:

While () statement is that all data can be displayed, and it is especially convenient to do not know the number of cycles, and the FOR () statement, which can output data from the beginning to the specified position to the specified position, the output display is displayed. It is used when data is available. Let's take a look at the programming instance:

Let's build a database to stand: The database name is: MyDB table name is: TBL.

With the following statement: Create Table Tal (IDX INT (3), URL Char (100), Freetext CHAR (100))

Several data can be inserted into the log table with a phpMyAdmin tool.

Programming start:

$ ID = mysql_connect ("localhost") or Die ("Unable to create a database link"); #Link Database

$ Result = mysql_db_query ("MyDB", "SELECT * FROM TBL", $ ID); # query results and stored in variables

$ ROWS = mysql_num_rows ($ result); # 得 出 数据 表 表 表 表 数 数 个 个;

Echo "

"; # Prepare to output in the form of a table

Echo ""; #

Insert the output statement in the above two sentences, corresponding to different cases, the output statement is divided into several cases:

If all data is output, use for () to do

For ($ i = 0; $ i <$ rows; $ i ) {

$ total = mysql_fetch_array ($ result);

Echo "

$ TOTAL [IDX] ";

}

Use while ()

While ($ TOTAL = mysql_fetch_array ($ result))

{echo

< / TD> ";

}

When we want to display the display, it is impossible to display all the data one-time display, so you can use for () to complete this task.

We assume that each output 10 data, use $ PAGE to represent the current number of pages $ PageSize = 10 to represent the number of pages of data. The statement is as follows:

For ($ I = 0; $ i <$ pageSize; $ i )

{

$ start = ($ PAGE-1) * $ PAGESIZE $ I; # Count the number of startup data lines

IF ($ Start <$ rows)

$ IDX = mysql_result ($ RESULT, $ START, "Idx");

$ URL = mysql_result ($ RESULT, $ START, "URL");

$ freetext = mysql_result ($ Result, $ start, "freetext"); echo "

The above statement uses for () to draw the value in the value of the fields in the data table, and display it with the ECHO statement.

The above programs run in Apache MySQL PHP4

转载请注明原文地址:https://www.9cbs.com/read-62976.html

New Post(0)
CopyRight © 2020 All Rights Reserved
Processed: 0.053, SQL: 9